How are variables created in Python? using pointers in Python

We have all studied the C Programming language and understand the ideas behind pointers. However is a quick recap.
Let us declare 2 variables in some program. The code would be
int a=3,b=4;
These 2 variables will get stored somewhere in memory.

Variable Name                Value                    Address  

a                                                                3                                        1000
b                                                                4                                        1002

C has 2 operators * and &. * is the value operator and gives the value at an address while & gives the address of a variable.
Let us declare two pointers now.
int *p1=&a,*p2=&b;
p1 = 1000 and p2=1002
*p1=3 and *p2=4

The equivalent statements in Python would be like this.

Program Sample

import _ctypes as ct
a,b=3,4
print(a,b)
p1,p2=id(a),id(b)
print(p1,p2)
print(ct.PyObj_FromPtr(p1),ct.PyObj_FromPtr(p2))

Varanasi Software Junction: Accessing pointers in Python using id and ctype




All variables are stored in the dir list. There is a collection of global variables and a collection of local variables i,e variables in a function 
Here is a program that accesses it
a,b=3,4
variables =dir()
print("Dir ",variables)
for variable in variables:
print(variable)
globalvariables=globals()
print("Global Variables")
for variable in globalvariables:
print(variable)
def f():
print("Local Variables")
la=2
lb=3
localvariables=locals()
for variable in localvariables:
print(variable)

f()


Output


Dir  ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b']
__annotations__
__builtins__
__cached__
__doc__
__file__
__loader__
__name__
__package__
__spec__
a
b
Global Variables
__name__
__doc__
__package__
__loader__
__spec__
__annotations__
__builtins__
__file__
__cached__
a
b
variables
variable
globalvariables
Local Variables
lb
la

All variables are available in the dictionary returned by vars().

Program Sample

import IPython as ip
a,b=1,2
variables=vars()
print(variables)
print(type(variables))
print(variables.keys())
print(variables.values())
variables.pop("a")
print(variables.keys())

 


{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000000CFCB62C160>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/Champak Roy/PycharmProjects/pythonProject3/chapter 2.py', '__cached__': None, 'ip': <module 'IPython' from 'H:\\Anaconda3\\lib\\site-packages\\IPython\\__init__.py'>, 'a': 1, 'b': 2, 'variables': {...}}
<class 'dict'>
dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__', '__cached__', 'ip', 'a', 'b', 'variables'])
dict_values(['__main__', None, None, <_frozen_importlib_external.SourceFileLoader object at 0x000000CFCB62C160>, None, {}, <module 'builtins' (built-in)>, 'C:/Users/Champak Roy/PycharmProjects/pythonProject3/chapter 2.py', None, <module 'IPython' from 'H:\\Anaconda3\\lib\\site-packages\\IPython\\__init__.py'>, 1, 2, {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000000CFCB62C160>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/Champak Roy/PycharmProjects/pythonProject3/chapter 2.py', '__cached__': None, 'ip': <module 'IPython' from 'H:\\Anaconda3\\lib\\site-packages\\IPython\\__init__.py'>, 'a': 1, 'b': 2, 'variables': {...}}])
dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__', '__cached__', 'ip', 'b', 'variables'])






Post a Comment

0 Comments

Me