Re: Finding the instance reference of an object



On Fri, 31 Oct 2008 16:02:53 +0000, Duncan Booth wrote:

pjacobi.de@xxxxxxxxxxxxxx wrote:

x = 1
y = x # does assignment make copies? y += 1
assert x == 1
=> succeeds, which implies that Python makes a copy when assigning

with lists:

x = [1]
y = x # does assignment make copies? y += [1]
assert x == [1]
=> fails, which implies that Python uses references when assigning

Compare lists with tupels:

x = (1,)
y = x # does assignment make copies? y += (1,)
assert x == (1,)
=> succeeds, which implies *what*?

All any of this does is 'implies that += may create a new object or may
mutate an existing object. RTFM: Python Reference Manual 6.3.1

The exact test isn't important. If you don't like those tests, replace
them with something else: y = y + [1] perhaps, or y.sort(), or whatever
you like. Naturally you will get different results according to whatever
specific test you try, and the interpretation of those results will
therefore be different. But no matter what tests are done, somebody who
fails to understand Python's calling model (or if you prefer, its
assignment model) will wrongly interpret the results they see in terms of
a model they do understand.

Because call by reference and call by value are such older and
established models, and used in such historically popular languages like
C and Pascal, they are the most likely incorrect assumptions people will
start from.


--
Steven
.



Relevant Pages

  • Re: Finding the instance reference of an object [long and probably boring]
    ... See, Python is call-by-reference!!! ... on "tricks" and indirect techniques as in the Python code above? ... trouble when, instead of assigning 1, you are assigning (a reference ... "Person foo;" declares a person reference. ...
    (comp.lang.python)
  • Re: Pythons garbage collection was Re: Python reliability
    ... >>> Has anyone looked into using a real GC for python? ... A strategy based on PURE reference counting just ... > extensions, and with correct implementations, both refcounting and GC are ... > Lucky those existing C libraries were written to use python's refcounting! ...
    (comp.lang.python)
  • More than you ever wanted to know about objects [was: Is everything a refrence or isnt it]
    ... Python has features for "introspection", which means that programs can examine the structure of the data rather than only dealing with values of known structure. ... C Python keeps a count of references to all objects, and when the reference count falls to zero it reclaims the ... Under the hood the interpreter looks for an attribute called "method" in the instance. ... If the type is defined as a specialisation of some other type (a "subclass" of the other type - "type2 is like type1 but with the following differences") then the interpreter will consult the other type, and so on and so on. ...
    (comp.lang.python)
  • Re: Finding the instance reference of an object
    ... in the Python docs, yet many here seem to want to deny it. ... Python names are references -- it's all over the place, from any discussion of "reference counting" to understanding the basics of what "a = b" does. ... This is a basic and fundamental thing that a programmer of a language should know. ... referring to a Bar. ...
    (comp.lang.python)
  • Re: Basic inheritance question
    ... Old Java habits die slowly. ... No, seriously it isn't Java habits only, most other languages wouldn't ... That's not very far from what a Python method object does - ... reference to the current instance is to pass it as an argument to the ...
    (comp.lang.python)