Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-26 Thread 88888 Dihedral
Michael Torrie於 2013年6月20日星期四UTC+8下午2時01分11秒寫道:
 
  But since the LISP never really got a form beyond S-expressions,
 
 leaving us with lots of parenthesis everywhere, Python wins much as the
 
 Hitchhiker's Guide to the Galaxy wins.

Yep, a list is mutable even it's empty.
But constant integers, floats, strings, and None is immutable.

The  variables in a function of python with default 
parameters which could be mutable or immutable.

def fun1( x, alist=[]):
alist.append(x*x)
return alist   ## valid

def fun2(x, alist=None):
if alist==None: alist=[]
alist.append(x*x)
return alist

# kind of boring to show the name binding mechanism of objects
# in Python in different usages
 




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-20 Thread Michael Torrie
On 06/19/2013 11:48 PM, Steven D'Aprano wrote:
 On Wed, 19 Jun 2013 23:16:51 -0600, Michael Torrie wrote:
 
 The real power and expressivity of Python comes from embracing the
 abstractions that Python provides to your advantage.  There's a certain
 elegance and beauty that comes from such things, which I believe really
 comes from the elegance and beauty of LISP, some of which manages to
 shine forth in Python, despite its deficiencies.  When I first learned
 Python, I was impressed that some of the elegance that I remember from
 Scheme (how to use lists as a basic type for example) was there, but in
 a form that appealed to me.
 
 
 Well said!

Glad you made sense of it... the bit about LISP and Scheme came out a
wee bit muddled.  In fact thinking about it, perhaps LISPers would say
about Python what a bible passage says about having the form of
Godliness but denying the power thereof!  For example, Python's lambda
functions, and Python's functional programming capabilities in general.
 But since the LISP never really got a form beyond S-expressions,
leaving us with lots of parenthesis everywhere, Python wins much as the
Hitchhiker's Guide to the Galaxy wins.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-20 Thread Roel Schroeven

Νίκος schreef:

Στις 18/6/2013 12:05 μμ, ο/η Steven D'Aprano έγραψε:

Names are *always* linked to objects, not to other names.

a = []
b = a  # Now a and b refer to the same list
a = {} # Now a refers to a dict, and b refers to the same list as before


I see, thank you Steven.

But since this is a fact how do you create complicated data structures 
that rely on various variables pointing one to another liek we did in 
C++(cannot recall their names) ?


You almost never need to do that in Python. But if you really want to, 
out of curiosity, you can. For example, you could create a simple singly 
linked list like this:


 class Node(object):
def __init__(self, value):
self.value = value
self.next = None


 first = Node(1)
 second = Node(2)
 first.next = second

You could iterate over it like this:

 def iterate_linked_list(node):
while node:
yield node.value
node = node.next


 for v in iterate_linked_list(first):
print v


--
People almost invariably arrive at their beliefs not on the basis of
proof but on the basis of what they find attractive.
-- Pascal Blaise

r...@roelschroeven.net

--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-19 Thread Michael Torrie
On 06/18/2013 03:51 AM, Νίκος wrote:
 Στις 18/6/2013 12:05 μμ, ο/η Steven D'Aprano έγραψε:
 Names are *always* linked to objects, not to other names.

 a = []
 b = a  # Now a and b refer to the same list
 a = {} # Now a refers to a dict, and b refers to the same list as before
 
 I see, thank you Steven.
 
 But since this is a fact how do you create complicated data structures 
 that rely on various variables pointing one to another liek we did in 
 C++(cannot recall their names) ?
 

As Steve said Python provides all manner of data structures and the
means to create data structures.  Any data structure (trees, lists, etc)
can all be made easily in Python using Python's basic data structures,
if the built-in data structures are not sufficient.  It turns out that
lists, hashes (dicts), and classes can pretty much do anything with
having to much about with C-style pointers and such.

Do yourself a favor and forget about the low-level stuff in Python for
now.  You'll be more frustrated if you don't.

The real power and expressivity of Python comes from embracing the
abstractions that Python provides to your advantage.  There's a certain
elegance and beauty that comes from such things, which I believe really
comes from the elegance and beauty of LISP, some of which manages to
shine forth in Python, despite its deficiencies.  When I first learned
Python, I was impressed that some of the elegance that I remember from
Scheme (how to use lists as a basic type for example) was there, but in
a form that appealed to me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-19 Thread Michael Torrie
On 06/19/2013 11:16 PM, Michael Torrie wrote:
 It turns out that lists, hashes (dicts), and classes can pretty much
 do anything with having to much about with C-style pointers and
 such.

Oh wow. Parse error.  should read, pretty much do anything without
having to muck about with C-style pointers and such.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-19 Thread Steven D'Aprano
On Wed, 19 Jun 2013 23:16:51 -0600, Michael Torrie wrote:

 The real power and expressivity of Python comes from embracing the
 abstractions that Python provides to your advantage.  There's a certain
 elegance and beauty that comes from such things, which I believe really
 comes from the elegance and beauty of LISP, some of which manages to
 shine forth in Python, despite its deficiencies.  When I first learned
 Python, I was impressed that some of the elegance that I remember from
 Scheme (how to use lists as a basic type for example) was there, but in
 a form that appealed to me.


Well said!


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-18 Thread Steven D'Aprano
On Tue, 18 Jun 2013 00:12:34 -0400, Dave Angel wrote:

 On 06/17/2013 10:42 PM, Steven D'Aprano wrote:
 On Mon, 17 Jun 2013 21:06:57 -0400, Dave Angel wrote:

 On 06/17/2013 08:41 PM, Steven D'Aprano wrote:

  SNIP

 In Python 3.2 and older, the data will be either UTF-4 or UTF-8,
 selected when the Python compiler itself is compiled.

 I think that was a typo.  Do you perhaps UCS-2 or UCS-4

 Yes, that would be better.

 UCS-2 is identical to UTF-16, except it doesn't support non-BMP
 characters and therefore doesn't have surrogate pairs.

 UCS-4 is functionally equivalent to UTF-16,
 
 Perhaps you mean UTF-32 ?


Yes, sorry for the repeated confusion.


   as far as I can tell. (I'm
 not really sure what the difference is.)


 Now you've got me curious, by bringing up surrogate pairs.  Do you know
 whether a narrow build (say 3.2) really works as UTF16, so when you
 encode a surrogate pair (4 bytes) to UTF-8, it encodes a single Unicode
 character into a single UTF-8 sequence (prob.  4 bytes long) ?

In a Python narrow build, the internal storage of strings is equivalent 
to UTF-16: all characters in the Basic Multilingual Plane require two 
bytes:

py sys.maxunicode
65535
py sys.getsizeof('π') - sys.getsizeof('')
2

Outside of the BMP, characters are treated as a pair of surrogates:

py c = chr(0x10F000)  # one character...
py len(c)  # ...stored as a pair of surrogates
2

Encoding and decoding works fine:

py c.encode('utf-8').decode('utf-8') == c
True
py c.encode('utf-8')
b'\xf4\x8f\x80\x80'


The problem with surrogates is that it is possible to accidentally 
separate the pair, which leads to broken, invalid text:

py c[0].encode('utf-8')
Traceback (most recent call last):
  File stdin, line 1, in module
UnicodeEncodeError: 'utf-8' codec can't encode character '\udbfc' in 
position 0: surrogates not allowed


(The error message is a little misleading; surrogates are allowed, but 
only if they make up a valid pair.)


Python's handling of UTF-16 is, as far as I know, correct. What isn't 
correct is that the high-level Python string methods assume that two 
bytes == one character, which can lead to surrogates being separated, 
which gives you junk text. Wide builds don't have this problem, because 
every character == four bytes, and neither does Python 3.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-18 Thread Larry Hudson

On 06/17/2013 08:50 AM, Simpleton wrote:

On 17/6/2013 2:58 μμ, Michael Torrie wrote:

a = 5
b = a

a --- memory address
b --- memory address

I like to think a and b as references to the same memory address

Not quite:  a and b _are_ memory addresses,  At the same time, a and b are references to the 
data (the objects) stored in those memory locations.


The distinction is probably more important in languages like C/C++, where the _language_ gives 
you direct access to, and can manipulate, these memory addresses (through pointers).  Python 
handles it differently and does not give you this sort of ability, it all occurs under the 
hood.  Yes, the id() function will tell you the addresses, but you can't do anything with them 
other than perhaps compare them.  It's really pretty much useless information.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-18 Thread Steven D'Aprano
On Mon, 17 Jun 2013 23:39:10 -0700, Larry Hudson wrote:

 On 06/17/2013 08:50 AM, Simpleton wrote:
 On 17/6/2013 2:58 μμ, Michael Torrie wrote:

 a = 5
 b = a

 a --- memory address
 b --- memory address

 I like to think a and b as references to the same memory address

 Not quite:  a and b _are_ memory addresses,  

Not in Python they aren't. a and b are names in a namespace.


 At the same time, a and b
 are references to the data (the objects) stored in those memory
 locations.

Not in Python they aren't. In Python, objects are free to move around 
memory. Not all implementations take advantage of this freedom, but some 
like Jython, IronPython and PyPy do.


 The distinction is probably more important in languages like C/C++,
 where the _language_ gives you direct access to, and can manipulate,
 these memory addresses (through pointers).  Python handles it
 differently and does not give you this sort of ability, it all occurs
 under the hood.  Yes, the id() function will tell you the addresses,
 but you can't do anything with them other than perhaps compare them. 
 It's really pretty much useless information.

The id() function does not tell you the address of the object, except by 
accident. The id() function gives you an arbitrary ID number for the 
object:


steve@runes:~$ ipy
IronPython 2.6 Beta 2 DEBUG (2.6.0.20) on .NET 2.0.50727.1433
Type help, copyright, credits or license for more information.
 id([])
43
 id('*')
44


steve@runes:~$ jython
Jython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19) 
[OpenJDK Client VM (Sun Microsystems Inc.)] on java1.6.0_18
Type help, copyright, credits or license for more information.
 id([])
1
 id('*')
2



That some implementations happen to use a fixed memory address as the ID 
number is, well, a mere accident of implementation. That's not what id() 
*is*, any more than id() returns the next value in an integer sequence 
starting from 43 just because that's what IronPython happens to do.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-18 Thread Νίκος

Στις 18/6/2013 9:39 πμ, ο/η Larry Hudson έγραψε:

Not quite:  a and b _are_ memory addresses,  At the same time, a and b
are references to the data (the objects) stored in those memory locations.

The distinction is probably more important in languages like C/C++,
where the _language_ gives you direct access to, and can manipulate,
these memory addresses (through pointers).  Python handles it
differently and does not give you this sort of ability, it all occurs
under the hood.  Yes, the id() function will tell you the addresses,
but you can't do anything with them other than perhaps compare them.
It's really pretty much useless information.


So, a and b are actual memory addresses.

Does the term of a pointer exist in Python?
I mean if print(a) or print(b) outputs the object that a and b are 
linked to, then how do we access a's and b's memory locations themselves 
t create links among variables, one pointing to the other and so on?


Can a variable point to another variable or variables never point to 
other variables but instead are *only* linked to the objects of those 
var's instead?



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-18 Thread Steven D'Aprano
On Tue, 18 Jun 2013 11:49:36 +0300, Νίκος wrote:

 Στις 18/6/2013 9:39 πμ, ο/η Larry Hudson έγραψε:
 Not quite:  a and b _are_ memory addresses,  At the same time, a and b
 are references to the data (the objects) stored in those memory
 locations.

 The distinction is probably more important in languages like C/C++,
 where the _language_ gives you direct access to, and can manipulate,
 these memory addresses (through pointers).  Python handles it
 differently and does not give you this sort of ability, it all occurs
 under the hood.  Yes, the id() function will tell you the addresses,
 but you can't do anything with them other than perhaps compare them.
 It's really pretty much useless information.
 
 So, a and b are actual memory addresses.

No, no, no, a thousand times no.


 Does the term of a pointer exist in Python? 

No.


 I mean if print(a) or
 print(b) outputs the object that a and b are linked to, then how do we
 access a's and b's memory locations themselves t create links among
 variables, one pointing to the other and so on?

You cannot. You can only have links between OBJECTS, not between 
VARIABLES. There is no way to have a name a set to point to another 
name b. All you can do is have a name a set to refer to the same 
object as b has *right now*. If b changes to another object, a will 
not follow.


 Can a variable point to another variable or variables never point to
 other variables but instead are *only* linked to the objects of those
 var's instead?

Names are *always* linked to objects, not to other names.

a = []
b = a  # Now a and b refer to the same list
a = {} # Now a refers to a dict, and b refers to the same list as before




-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-18 Thread Νίκος

Στις 18/6/2013 12:05 μμ, ο/η Steven D'Aprano έγραψε:

Names are *always* linked to objects, not to other names.

a = []
b = a  # Now a and b refer to the same list
a = {} # Now a refers to a dict, and b refers to the same list as before


I see, thank you Steven.

But since this is a fact how do you create complicated data structures 
that rely on various variables pointing one to another liek we did in 
C++(cannot recall their names) ?


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-18 Thread Chris Angelico
On Tue, Jun 18, 2013 at 7:51 PM, Νίκος supp...@superhost.gr wrote:
 Στις 18/6/2013 12:05 μμ, ο/η Steven D'Aprano έγραψε:

 Names are *always* linked to objects, not to other names.

 a = []
 b = a  # Now a and b refer to the same list
 a = {} # Now a refers to a dict, and b refers to the same list as before


 I see, thank you Steven.

 But since this is a fact how do you create complicated data structures that
 rely on various variables pointing one to another liek we did in C++(cannot
 recall their names) ?

Why do you need to? Linked lists, trees, and so on are just tools.
They're usually used to implement data structures like mappings,
growable arrays, lists that can have elements inserted into them, etc,
etc. Python does these sorts of things in better ways. You should not
need to worry about memory locations, pointers, etc. Now, if you want
to have one object reference another, that can be done in plenty of
ways. Check the Python tutorial.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Simpleton

On 17/6/2013 9:51 πμ, Steven D'Aprano wrote:

Now, in languages like Python, Ruby, Java, and many others, there is no
table of memory addresses. Instead, there is a namespace, which is an
association between some name and some value:

global namespace:
 x -- 23
 y -- hello world


First of all thanks for the excellent and detailed explanation Steven.

As for namespace:

a = 5

1. a is associated to some memory location
2. the latter holds value 5

So 'a', is a reference to that memory location, so its more like a name 
to that memory location, yes? Instead of accessing a memory address with 
a use of an integer like 14858485995 we use 'a' instead.


So is it safe to say that in Python a == a ? ( stands for memory address)

is the above correct?

I say this because here you said that: Instead, there is a namespace, 
which is anassociation between some name and some value:


When you say that you mean that a is associated to some value as in 
memory location or to that memory location's address?




--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Michael Torrie
On 06/17/2013 05:34 AM, Simpleton wrote:
 So is it safe to say that in Python a == a ? ( stands for memory address)
 
 is the above correct?

It might be partially equivalent inside the interpreter, but it's not
something you should concern yourself with.  And in general, no it's not
safe to say, since Python is a reference-counted, garbage-collected
object system and pointers in C certainly are not.

 I say this because here you said that: Instead, there is a namespace, 
 which is anassociation between some name and some value:
 
 When you say that you mean that a is associated to some value as in 
 memory location or to that memory location's address?

In python just think of assignment as making a name *be* an object.  And
if you assign one name to another name, that makes both names be the
same object.  When names are unbound (either they go out of scope or you
manually unbind them), the objects they are bound to are garbage collected.

Forget about the details of how the interpreter might doing at a low level.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Terry Reedy

On 6/17/2013 7:34 AM, Simpleton wrote:

On 17/6/2013 9:51 πμ, Steven D'Aprano wrote:

Now, in languages like Python, Ruby, Java, and many others, there is no
table of memory addresses. Instead, there is a namespace, which is an
association between some name and some value:

global namespace:
 x -- 23
 y -- hello world


First of all thanks for the excellent and detailed explanation Steven.

As for namespace:

a = 5

1. a is associated to some memory location
2. the latter holds value 5


This is backwards. If the interpreter puts 5 in a *permanent* 'memory 
location' (which is not required by the language!), then it can 
associate 'a' with 5 by associating it with the memory location. CPython 
does this, but some other computer implementations do not.



So 'a', is a reference to that memory location, so its more like a name
to that memory location, yes? Instead of accessing a memory address with
a use of an integer like 14858485995 we use 'a' instead.

So is it safe to say that in Python a == a ? ( stands for memory address)

is the above correct?


When you interpret Python code, do you put data in locations with 
integer addresses?


--
Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Simpleton

On 17/6/2013 2:58 μμ, Michael Torrie wrote:

In python just think of assignment as making a name *be* an object.  And
if you assign one name to another name, that makes both names be the
same object.  When names are unbound (either they go out of scope or you
manually unbind them), the objects they are bound to are garbage collected.


Object here being the memory location, right?
When we say a = 5

a = an easy way for calling that fixed memory location that holds our 
value, instead of calling it in binary format or in hex format.

This is the direct object a is pointing too. Correct?

5 = *this* is the indirect object that a outputs when we print a.

Are the above statements correct Michael?

a = 5
b = a

a --- memory address
b --- memory address

I like to think a and b as references to the same memory address



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Simpleton

On 17/6/2013 5:22 μμ, Terry Reedy wrote:

On 6/17/2013 7:34 AM, Simpleton wrote:

On 17/6/2013 9:51 πμ, Steven D'Aprano wrote:

Now, in languages like Python, Ruby, Java, and many others, there is no
table of memory addresses. Instead, there is a namespace, which is an
association between some name and some value:

global namespace:
 x -- 23
 y -- hello world


First of all thanks for the excellent and detailed explanation Steven.

As for namespace:

a = 5

1. a is associated to some memory location
2. the latter holds value 5


This is backwards. If the interpreter puts 5 in a *permanent* 'memory
location' (which is not required by the language!), then it can
associate 'a' with 5 by associating it with the memory location. CPython
does this, but some other computer implementations do not.


Please tell me how do i need to understand the sentence
'a' is being associated with number 5 in detail.

Why don't we access the desired value we want to, by referencing to that 
value's memory location directly instead of using namespaces wich is an 
indirect call?


i feel we have 3 things here

a , memory address of a stored value, actual stored value


So is it safe to say that in Python a == a ? ( stands for memory
address)

is the above correct?


When you interpret Python code, do you put data in locations with
integer addresses?


I lost you here.


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Joel Goldstick
On Mon, Jun 17, 2013 at 11:55 AM, Simpleton supp...@superhost.gr wrote:

 On 17/6/2013 5:22 μμ, Terry Reedy wrote:

 On 6/17/2013 7:34 AM, Simpleton wrote:

 On 17/6/2013 9:51 πμ, Steven D'Aprano wrote:

 Now, in languages like Python, Ruby, Java, and many others, there is no
 table of memory addresses. Instead, there is a namespace, which is an
 association between some name and some value:

 global namespace:
  x -- 23
  y -- hello world


 First of all thanks for the excellent and detailed explanation Steven.

 As for namespace:

 a = 5

 1. a is associated to some memory location
 2. the latter holds value 5


 This is backwards. If the interpreter puts 5 in a *permanent* 'memory
 location' (which is not required by the language!), then it can
 associate 'a' with 5 by associating it with the memory location. CPython
 does this, but some other computer implementations do not.


 Please tell me how do i need to understand the sentence
 'a' is being associated with number 5 in detail.

 Why don't we access the desired value we want to, by referencing to that
 value's memory location directly instead of using namespaces wich is an
 indirect call?

 i feel we have 3 things here

 a , memory address of a stored value, actual stored value

  So is it safe to say that in Python a == a ? ( stands for memory
 address)

 is the above correct?


 When you interpret Python code, do you put data in locations with
 integer addresses?


 I lost you here.



 --
 What is now proved was at first only imagined!
 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list



Read and study this.  Then come back and ask again.  Don't think of
physical representation of memory with actual binary addresses.  Python is
not assembler.  Neither is it C (sometimes called high level assembler)
http://docs.python.org/2/tutorial/classes.html#python-scopes-and-namespaces

-- 
Joel Goldstick
http://joelgoldstick.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Benjamin Kaplan
On Mon, Jun 17, 2013 at 8:55 AM, Simpleton supp...@superhost.gr wrote:
 On 17/6/2013 5:22 μμ, Terry Reedy wrote:

 On 6/17/2013 7:34 AM, Simpleton wrote:

 On 17/6/2013 9:51 πμ, Steven D'Aprano wrote:

 Now, in languages like Python, Ruby, Java, and many others, there is no
 table of memory addresses. Instead, there is a namespace, which is an
 association between some name and some value:

 global namespace:
  x -- 23
  y -- hello world


 First of all thanks for the excellent and detailed explanation Steven.

 As for namespace:

 a = 5

 1. a is associated to some memory location
 2. the latter holds value 5


 This is backwards. If the interpreter puts 5 in a *permanent* 'memory
 location' (which is not required by the language!), then it can
 associate 'a' with 5 by associating it with the memory location. CPython
 does this, but some other computer implementations do not.


 Please tell me how do i need to understand the sentence
 'a' is being associated with number 5 in detail.

 Why don't we access the desired value we want to, by referencing to that
 value's memory location directly instead of using namespaces wich is an
 indirect call?

 i feel we have 3 things here

 a , memory address of a stored value, actual stored value

 So is it safe to say that in Python a == a ? ( stands for memory
 address)

 is the above correct?


 When you interpret Python code, do you put data in locations with
 integer addresses?


 I lost you here.


You're confusing the way in which the CPython interpreter is written
with the way the Python language is defined. Yes, the CPython
interpreter puts 5 into a numbered memory location when creating the
object. But the Nikos Python Interpreter (which is a completely valid,
although quite buggy, Python interpreter that uses your head to
interpret the code) should not be using numbered memory locations.

Forget about memory locations. Memory locations don't exist. Let's use
the pen-and-paper Python interpreter. On the left side of the paper,
we'll write down the names. On the rest of the paper, we'll write down
the objects.

When I do x =[], I make a new list (which means I write down []
somewhere in the middle of the page), I create the name x (which
means I write down an x on the left side of the page), and then I
draw an arrow pointing from the x to the [].
(sorry if my drawing doesn't line up in your email/news client- I'll
try to make it line up correctly with a monospace font)

|  x -- []|
|  |
|  |


Now, When we do y = x, the right side of the equation is evaluated
(which gives us the object currently bound to the name x) and it is
then given the newly created name y

|  x -- []|
| ^|
|   y ||


When we do x.append(3), the object that x refers to is modified. This
is seen by both x and y because they point to the same object.


|  x -- [3]   |
| ^|
|   y ||


When I do x = [], a new object is created somewhere else on the page,
and the name x is made to point to the new object. This changes the
arrow. The name x is not in any way tied to the location of the [3]
on the page. This doesn't impact y which is still pointing at the
same spot it was before


|  x [] [3]   |
| ^|
|   y ||


That is how Python's object model works. In CPython, objects have
specified memory locations but names do not. In IronPython and Jython,
even that's not guaranteed- the garbage collector can move the objects
around during their lifetime, so while you can trust that a name will
always point to the correct object, you have no guarantee about where
the object is located in the computer's memory.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Νίκος

On 17/6/2013 7:23 μμ, Benjamin Kaplan wrote:

On Mon, Jun 17, 2013 at 8:55 AM, Simpleton supp...@superhost.gr wrote:

On 17/6/2013 5:22 μμ, Terry Reedy wrote:


On 6/17/2013 7:34 AM, Simpleton wrote:


On 17/6/2013 9:51 πμ, Steven D'Aprano wrote:


Now, in languages like Python, Ruby, Java, and many others, there is no
table of memory addresses. Instead, there is a namespace, which is an
association between some name and some value:

global namespace:
  x -- 23
  y -- hello world



First of all thanks for the excellent and detailed explanation Steven.

As for namespace:

a = 5

1. a is associated to some memory location
2. the latter holds value 5



This is backwards. If the interpreter puts 5 in a *permanent* 'memory
location' (which is not required by the language!), then it can
associate 'a' with 5 by associating it with the memory location. CPython
does this, but some other computer implementations do not.



Please tell me how do i need to understand the sentence
'a' is being associated with number 5 in detail.

Why don't we access the desired value we want to, by referencing to that
value's memory location directly instead of using namespaces wich is an
indirect call?

i feel we have 3 things here

a , memory address of a stored value, actual stored value


So is it safe to say that in Python a == a ? ( stands for memory
address)

is the above correct?



When you interpret Python code, do you put data in locations with
integer addresses?



I lost you here.



You're confusing the way in which the CPython interpreter is written
with the way the Python language is defined. Yes, the CPython
interpreter puts 5 into a numbered memory location when creating the
object. But the Nikos Python Interpreter (which is a completely valid,
although quite buggy, Python interpreter that uses your head to
interpret the code) should not be using numbered memory locations.

Forget about memory locations. Memory locations don't exist. Let's use
the pen-and-paper Python interpreter. On the left side of the paper,
we'll write down the names. On the rest of the paper, we'll write down
the objects.

When I do x =[], I make a new list (which means I write down []
somewhere in the middle of the page), I create the name x (which
means I write down an x on the left side of the page), and then I
draw an arrow pointing from the x to the [].
(sorry if my drawing doesn't line up in your email/news client- I'll
try to make it line up correctly with a monospace font)

|  x -- []|
|  |
|  |


Now, When we do y = x, the right side of the equation is evaluated
(which gives us the object currently bound to the name x) and it is
then given the newly created name y

|  x -- []|
| ^|
|   y ||


When we do x.append(3), the object that x refers to is modified. This
is seen by both x and y because they point to the same object.


|  x -- [3]   |
| ^|
|   y ||


When I do x = [], a new object is created somewhere else on the page,
and the name x is made to point to the new object. This changes the
arrow. The name x is not in any way tied to the location of the [3]
on the page. This doesn't impact y which is still pointing at the
same spot it was before


|  x [] [3]   |
| ^|
|   y ||


That is how Python's object model works. In CPython, objects have
specified memory locations but names do not. In IronPython and Jython,
even that's not guaranteed- the garbage collector can move the objects
around during their lifetime, so while you can trust that a name will
always point to the correct object, you have no guarantee about where
the object is located in the computer's memory.


EXCELLENT EXPLANATION, THANK YOU VERY MUCH.
I AM SAVING THIS TO A .TXT FOR FUTURE REFERENCE.

THAT'S WHY I ASK YOU GUYS FOR HUMAN LIKE EXPLANATIONS.
THIS CANNOT BE FIND WITH SUCH SIMPLE WAY OF PUTTING THIS IN TECH DOCS.

Something else please:

The way some info(i.e. a Unicode string) is saved into the hdd , is the 
same way its being saved into the memory too? Same way exactly?


While you said to me to forget about memory locations, and that's indeed 
made things easy to follow i still keep wondering, how Python internally 
keeping tracks of 'x' and 'y' names as well as their referenced 

Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Terry Reedy

On 6/17/2013 1:17 PM, Νίκος wrote:

On Mon, Jun 17, 2013 at 8:55 AM, Simpleton supp...@superhost.gr wrote:

On 17/6/2013 5:22 μμ, Terry Reedy wrote:



When you interpret Python code, do you put data in locations with
integer addresses?



I lost you here.


Memory in biological brains is not a linear series of bits, or 
characters. How we do associate things is still mostly a puzzle.


Read about holographic memory.


The way some info(i.e. a Unicode string) is saved into the hdd , is the
same way its being saved into the memory too? Same way exactly?


No. A unicode string is a sequence of abstract characters or codepoints. 
They must be encoded somehow to map them to linear byte memory. There 
are still (too) many encodings in use. Most cannot encode *all* unicode 
characters.


CPython is unusual in using one of three different encodings for 
internal unicode strings.



While you said to me to forget about memory locations,


This is excellent advice. One of the *features* of Python is that one 
*can* forget about addresses. One of the *problems* of C is that many 
people *do* forget about memory locations, while virus writers study 
them carefully.


--
Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Steven D'Aprano
On Mon, 17 Jun 2013 14:34:57 +0300, Simpleton wrote:

 On 17/6/2013 9:51 πμ, Steven D'Aprano wrote:
 Now, in languages like Python, Ruby, Java, and many others, there is no
 table of memory addresses. Instead, there is a namespace, which is an
 association between some name and some value:

 global namespace:
  x -- 23
  y -- hello world
 
 First of all thanks for the excellent and detailed explanation Steven.
 
 As for namespace:
 
 a = 5
 
 1. a is associated to some memory location 

No. a is associated to an object, the int 5, which may be free to move in 
memory (PyPy does this), or may be at a fixed memory location (CPython 
does this). But the association is with the object, not the memory 
location.

The object is a data structure that contains a type (int), a value (5), 
and various methods (e.g. bit_length, to_bytes).


2. the latter holds value 5
 
 So 'a', is a reference to that memory location, so its more like a name
 to that memory location, yes? Instead of accessing a memory address with
 a use of an integer like 14858485995 we use 'a' instead.

No. We're talking about what the Python interpreter does, not what you 
do. Whether you are programming in C or Python, you still refer to 
variables by name:

a = 5

but what goes on inside the interpreter or compiler is different.


 So is it safe to say that in Python a == a ? ( stands for memory
 address)

Absolutely not.


 is the above correct?
 
 I say this because here you said that: Instead, there is a namespace,
 which is anassociation between some name and some value:
 
 When you say that you mean that a is associated to some value as in
 memory location or to that memory location's address?

No. This has nothing to do with memory locations. Namespaces are dicts. 
Write down a dict:

{a: Hello world}

Do you see a memory location there? There is no memory location. There is 
the name, a, and the object it is associated with, Hello world. 
Either the dict, or the string, may move around memory if the underlying 
memory manager allows it. Obviously any object in Python must be 
*somewhere* in memory at any specific moment, but that is irrelevant to 
understanding Python code. It is no more relevant than saying that a dict 
{'a': 5} is just made up of bits.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Νίκος

Στις 18/6/2013 2:09 πμ, ο/η Steven D'Aprano έγραψε:

{a: Hello world}

Do you see a memory location there? There is no memory location. There is
the name, a, and the object it is associated with, Hello world.
Either the dict, or the string, may move around memory if the underlying
memory manager allows it. Obviously any object in Python must be
*somewhere* in memory at any specific moment, but that is irrelevant to
understanding Python code. It is no more relevant than saying that a dict
{'a': 5} is just made up of bits.


Okey, the easy part was to understand how humans in high level need to 
understand namespaces and assignment (which is not copying but instead 
referencing another name to the same object).


But i would like to know, what happens internally within the python 
compiler, because obviously memory is involved.


The way some info(i.e. a Unicode string) is saved into the hdd , is the 
same way its being saved into the memory too? Same way exactly?


While you said to me to forget about memory locations, and that's indeed 
made things easy to follow i still keep wondering, how Python internally 
keeping tracks of 'x' and 'y' names as well as their referenced objects 
(i.e. number 6).


After all the way i understand memory is as a series of bits like:

0100010100001011010101001000100101001001111101001010010

So from the above binary form:

what is 'x', what is 'y', how's 'x' and 'y' differ from the actually 
memory locations that are bound too, and of course what is the actual value.


Its 3 things for me to consider, even in Python id internal level 
detail. I want to understand this.


names, memory addresses, memory address's actual values

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Steven D'Aprano
On Tue, 18 Jun 2013 02:26:39 +0300, Νίκος wrote:

 Στις 18/6/2013 2:09 πμ, ο/η Steven D'Aprano έγραψε:
 {a: Hello world}

 Do you see a memory location there? There is no memory location. There
 is the name, a, and the object it is associated with, Hello world.
 Either the dict, or the string, may move around memory if the
 underlying memory manager allows it. Obviously any object in Python
 must be *somewhere* in memory at any specific moment, but that is
 irrelevant to understanding Python code. It is no more relevant than
 saying that a dict {'a': 5} is just made up of bits.
 
 Okey, the easy part was to understand how humans in high level need to
 understand namespaces and assignment (which is not copying but instead
 referencing another name to the same object).
 
 But i would like to know, what happens internally within the python
 compiler, because obviously memory is involved.

Depends which Python compiler.

CPython, IronPython, PyPy, Jython, Nuitka, Cython, ... ? They all operate 
differently on the inside. Only the high-level Python code has to be the 
same.

Jython works according to the JRE, the Java Runtime Environment. There is 
masses of information about that on the Internet, google for it.

IronPython works according to the .Net runtime environment. Again, google 
for it.

PyPy is a VERY complex but powerful JIT compiler. You can read about it 
on the PyPy blog. Start here: http://morepypy.blogspot.com/

There's not a lot of documentation on how CPython works internally. You 
have to read the C source code. You will need to understand about garbage 
collectors, memory management, the difference between the stack and the 
heap, etc. It's a big area to study. Don't expect anyone to spend the 
days or weeks it will take to explain the whole thing.


 The way some info(i.e. a Unicode string) is saved into the hdd , is the
 same way its being saved into the memory too? Same way exactly?

No. Unicode strings can be stored on disk in any encoding you like. 
Python stores string in memory as objects, which might look something 
like this:

[Type=str, size=42, data=...]

only more complicated. And subject to change -- anything you learn that 
holds for Python 3.3 might not apply for 3.2 or 3.4.

In Python 3.2 and older, the data will be either UTF-4 or UTF-8, selected 
when the Python compiler itself is compiled. In Python 3.3, the data will 
be stored in either Latin-1, UTF-4, or UTF-8, depending on the contents 
of the string.


 While you said to me to forget about memory locations, and that's indeed
 made things easy to follow i still keep wondering, how Python internally
 keeping tracks of 'x' and 'y' names as well as their referenced objects
 (i.e. number 6).

Good question, but I don't have a good answer. It has to do with the 
Python's memory manager, and the garbage collector. I don't know how they 
work.



 After all the way i understand memory is as a series of bits like:
 
 0100010100001011010101001000100101001001111101001010010
 
 So from the above binary form:
 
 what is 'x', what is 'y', how's 'x' and 'y' differ from the actually
 memory locations that are bound too, and of course what is the actual
 value.
 
 Its 3 things for me to consider, even in Python id internal level
 detail. I want to understand this.
 
 names, memory addresses, memory address's actual values


Start here:

https://duckduckgo.com/html/?q=how%20computers%20work



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Dave Angel

On 06/17/2013 08:41 PM, Steven D'Aprano wrote:


SNIP

In Python 3.2 and older, the data will be either UTF-4 or UTF-8, selected
when the Python compiler itself is compiled.


I think that was a typo.  Do you perhaps UCS-2 or UCS-4


In Python 3.3, the data will
be stored in either Latin-1, UTF-4, or UTF-8, depending on the contents
of the string.




--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Steven D'Aprano
On Tue, 18 Jun 2013 00:41:53 +, Steven D'Aprano wrote:

 In Python 3.2 and older, the data will be either UTF-4 or UTF-8,
 selected when the Python compiler itself is compiled. In Python 3.3, the
 data will be stored in either Latin-1, UTF-4, or UTF-8, depending on the
 contents of the string.

UTF-4? UTF-8?

Whatever crack I was smoking, it obviously was *bad* crack.

That should be UTF-8 or UTF-16.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Steven D'Aprano
On Mon, 17 Jun 2013 21:06:57 -0400, Dave Angel wrote:

 On 06/17/2013 08:41 PM, Steven D'Aprano wrote:

 SNIP

 In Python 3.2 and older, the data will be either UTF-4 or UTF-8,
 selected when the Python compiler itself is compiled.
 
 I think that was a typo.  Do you perhaps UCS-2 or UCS-4

Yes, that would be better.

UCS-2 is identical to UTF-16, except it doesn't support non-BMP 
characters and therefore doesn't have surrogate pairs.

UCS-4 is functionally equivalent to UTF-16, as far as I can tell. (I'm 
not really sure what the difference is.)


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Steven D'Aprano
On Tue, 18 Jun 2013 02:38:20 +, Steven D'Aprano wrote:

 On Tue, 18 Jun 2013 00:41:53 +, Steven D'Aprano wrote:
 
 In Python 3.2 and older, the data will be either UTF-4 or UTF-8,
 selected when the Python compiler itself is compiled. In Python 3.3,
 the data will be stored in either Latin-1, UTF-4, or UTF-8, depending
 on the contents of the string.
 
 UTF-4? UTF-8?
 
 Whatever crack I was smoking, it obviously was *bad* crack.
 
 That should be UTF-8 or UTF-16.

Good lord, that crack is worse than I thought.

UTF-16 and UTF-32. 

Bloody hell. I am ashamed.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Marcin Szamotulski
 While you said to me to forget about memory locations, and that's indeed 
 made things easy to follow i still keep wondering, how Python internally 
 keeping tracks of 'x' and 'y' names as well as their referenced objects 
 (i.e. number 6).

There is an excellent blog post about CPython internals:
http://tech.blog.aknin.name/category/my-projects/pythons-innards/
but it might be difficult to follow if you cannot at least read C.
It explains how python virtual machine works internally, how the opcodes
are evaluated, how the three scopes globals, locals and builins find its
place (and how names are bind).  As far as I understand names are keys
in the scope dictionaries, which are exposed in the python level as
locals(), globals() and the builtin's.  This is how Python tracks names
and their values.  To be more precise, when you do:

 a = 1
 def f():
...  b=2

in the first line 'a' is added to the globals() dictionary with value 1,
and in the third line 'b' with value 2 is added to the local dictionary
of f.  It is also all explained in the python docs, and it reflects how
python is implemented (at least CPython).

 
 After all the way i understand memory is as a series of bits like:
 
 0100010100001011010101001000100101001001111101001010010
 
 So from the above binary form:
 
 what is 'x', what is 'y', how's 'x' and 'y' differ from the actually 
 memory locations that are bound too, and of course what is the actual value.

'x' and 'y' are just strings which are written somewhere in the computer's
memory.

 
 Its 3 things for me to consider, even in Python id internal level 
 detail. I want to understand this.
 
 names, memory addresses, memory address's actual values

Names and values are not connected through their memory addresses but
because they live in a higher structure, name is a key and value is a value
of a dictionary (which is also represented in some way at the C level,
namely by PyDictObject ... but for this you need to first learn C, but
please read and understand the Python docs - there is already a lot
there for you ...

Best regards,
Marcin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

2013-06-17 Thread Dave Angel

On 06/17/2013 10:42 PM, Steven D'Aprano wrote:

On Mon, 17 Jun 2013 21:06:57 -0400, Dave Angel wrote:


On 06/17/2013 08:41 PM, Steven D'Aprano wrote:


 SNIP

In Python 3.2 and older, the data will be either UTF-4 or UTF-8,
selected when the Python compiler itself is compiled.


I think that was a typo.  Do you perhaps UCS-2 or UCS-4


Yes, that would be better.

UCS-2 is identical to UTF-16, except it doesn't support non-BMP
characters and therefore doesn't have surrogate pairs.

UCS-4 is functionally equivalent to UTF-16,


Perhaps you mean UTF-32 ?

 as far as I can tell. (I'm

not really sure what the difference is.)



Now you've got me curious, by bringing up surrogate pairs.  Do you know 
whether a narrow build (say 3.2) really works as UTF16, so when you 
encode a surrogate pair (4 bytes) to UTF-8, it encodes a single Unicode 
character into a single UTF-8 sequence (prob.  4 bytes long) ?




--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list