Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Nov 22 15:12:26 CET 2013


On Fri, 22 Nov 2013 18:22:29 +0530, Bharath Kummar wrote:

> 1)  Is it possible to Retrieve the address of a variable in python ? 

No. Variables in Python are not at fixed addresses, like in Pascal or C, 
they are names in a namespace.

You can read this post for some more information about the difference 
between C variables and Python variables, and calling conventions across 
different languages:

https://mail.python.org/pipermail/tutor/2010-December/080505.html

It's a long post, but to summarise the part about variables:

In languages like Pascal or C, the compiler keeps a table mapping 
variable names to fixed memory addresses, like this:

Variable  Address
========  =======
x         10234
y         10238
z         10242

The command "x = 42" stores the value 42 into memory address 10234. If 
you ask the compiler for the address of x, it can say 10234. That's how 
variables work in languages like Pascal, C, Fortran, and similar.

With the Pascal or C style variable, the variable address exists even 
before you give it a value.

But languages like Python don't work that way. There is no table of 
variable:address available to the compiler, and variables don't have an 
address. Python's variables are *name bindings*, not fixed memory 
addresses. The Python runtime keeps a global dictionary which maps names 
to their values:

{'x': <integer object 42>,
 'y': <string object 'hello world'>,
 'z': <list object [1,2,3]>,
}

The general name for this is "namespace". In Python you can access the 
global namespace with the globals() function, and a read-only copy of the 
local namespace with the locals() function.

Entries in the namespace cannot be blank. So names don't exist before 
they are bound to a value.


> 2) 
> Is it possible to Delete the Address of the Variable and create a new
> dynamic address inside the compiler/interpreter itself ? 

I don't understand this question.

Since variables don't have addresses, you can't delete what doesn't exist.


> 3)  Is it easy
> to find the Binary equivalence of a given Alphanumeric String ?

Which binary equivalence are you referring to? Again, I don't understand 
your question. I can do this:

py> astring = "1234"
py> int(astring).to_bytes(4, 'big')
b'\x00\x00\x04\xd2'
py> int(astring).to_bytes(4, 'little')
b'\xd2\x04\x00\x00'


Or I can do this:

py> astring = "Alpha1234 δθЖ∞"
py> astring.encode('utf-8')
b'Alpha1234 \xce\xb4\xce\xb8\xd0\x96\xe2\x88\x9e'


Or I can do this:

py> import binascii
py> binascii.hexlify(b'Hello World!')
b'48656c6c6f20576f726c6421'


And many other string -> binary equivalences. Which ones did you have in 
mind?


> 4)  Is it possible to count the number of 1's in the Binary equivalence
> ?

Of course. First decide which binary equivalence you want, then decide 
what you mean by "count the number of 1s" (do you mean the byte with 
value 1, or the ASCII code for 1, or the bit 1?), then count them.


> Could you PLEASE provide me with the codes (codes only for the asked
> queries) ?

If you explain your question in more detail, we can give more detailed 
answers.



-- 
Steven



More information about the Python-list mailing list