Newbie look at Python and OO

Grant Edwards grante at visi.com
Thu May 10 12:25:35 EDT 2007


On 2007-05-10, walterbyrd <walterbyrd at iname.com> wrote:


> 2) list assignment handling, pointing two vars to the same list:

Python doesn't have variables.  It has objects to which you can
bind names.

> But with lists:
>>>> a = list("1234")

That creates a list object and binds the name "a" to it.

>>>> b = a

That binds the name b to that same object.

You now have two names bound a single object.

>>>> a.append("5")

Now you modify that object.

>>>> a,b
> (['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'])
>
> b  changes even though I have not touched b.

You modified the object to which the name "b" is bound.

> I know why, but this is not what I would ordinarilly expect,

Stop thinking in "C". ;)

> it does not seem intuitive.

When one attempts to use intuition devloped in one environment
in a different environment it is often wrong.  That's why
brains have capability to change.

> And,
> IMO, it gets worse:
>
>>>> a = list("1234")
>>>> b = a

Again, you have a single object to which both names "a" and "b"
are bound.

>>>> a = a + ['5']

Now you've created a new object and bound the name "a" to it.
You now have two objects and the name "b" is still bound to the
original one.

>>>> a,b
> (['1', '2', '3', '4', '5'], ['1', '2', '3', '4'])

> Sometimes changing a changes b, and sometimes not.

You're thinking wrongly.  Start thinking about objects/names
and not about "variables".

> You also have to remember that subseqent changes to a will not
> change b - after some operation but not others. To those who
> think in Python, I'm sure this all seems normal.

Yes, it does.

> But, having programmed in about one dozen other language, this
> seems downright bizare to me. I know why it works like this,
> but it seems like an odd way to do things.

If those dozen other languages all used the "variable" paradigm
(fixed regions of storage with fixed names) rather than the
object/name binding paradigm, then yes, it probably seems like
an odd way to do things.  

If you grew up using base-12, then base-10 seems like an odd
way to do things.

> 3) ambiguous use of the form: this.that()

Nope, it always means exactly the same thing: look up the
"that" attribute of object "this", and then call it.  Are you
confused by the fact that modules are objects?

> Sometimes, this.that() means module.funcion() as in:
>
>>>> os.dirlist(".")

Call the "dirlist" attribute of the object to which the name
"os" is bound.

> Other times, "this" is sort of like a parameter to the "that"
> function:
>
>>>> a = list("1234")
>>>> "_".join(a)
> '1_2_3_4_5'

Call the "join" attribute of the anonymous string object
"_".

> And still other times, is seems that "this" is an object, acted upon
> by "that" :
>
>>>> a = list("1234")
>>>> b = "_".join(a)
>>>> b.split("_")
> ['1', '2', '3', '4', '5']

Call the "split" attribute of the object to which the name "b"
is bound.

> Again, those who think in Python, will understand right away that:
>
> math.count(x)
>
> is counting the substring "x" in the "math" string.

No, that isn't obvious at all unless we've previously been told
that the name "math" is bound to a string object and we happen
to know that a string object's "count" attribute is a bound
method that counts occurances of the passed substring.

> But can you see where that might be confused to be a function
> called count() in the math module?

A python programmer would think that it's calling the "count"
attribute of whatever the name "math" is bound to.  The name
"math" might be bound to a module, a string, or some other type
of object.

> I'm not complaining. Python is a great language in many
> respects. But, I would take some issue with those claiming
> Python is intuitive and easy.

Unlearning old habits is always difficult.

> IMO: there seems to be many ambiguous, unintuitve, and
> confusing, aspects to Python.

Certainly if you're thinking about it using the wrong set of
concepts.

-- 
Grant Edwards                   grante             Yow! I'm continually AMAZED
                                  at               at th'breathtaking effects
                               visi.com            of WIND EROSION!!



More information about the Python-list mailing list