Newbie look at Python and OO

Larry Bates larry.bates at websafe.com
Thu May 10 12:38:20 EDT 2007


walterbyrd wrote:
> I learned to program with Pascal, way back when. Went into software
> development for a while, then went into systems admin. Have programmed
> in several languages, just learning Python.
> 
> Some things I find odd:
> 
> 1) 5/-2 == -3?
> 
> 2) list assignment handling, pointing two vars to the same list:
> 
> With simple data types:
>>>> a = 5
>>>> b = a
>>>> a = 3
>>>> a,b
> (3, 5)
> 
> Which is what I'd expect, since I have changed a, but not b.
> 
> But with lists:
>>>> a = list("1234")
>>>> b = a
>>>> a.append("5")
>>>> a,b
> (['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'])
> 
> b  changes even though I have not touched b. I know why, but this is
> not what I would ordinarilly expect, it does not seem intuitive. And,
> IMO, it gets worse:
> 
>>>> a = list("1234")
>>>> b = a
>>>> a = a + ['5']
>>>> a,b
> (['1', '2', '3', '4', '5'], ['1', '2', '3', '4'])
> 
> Sometimes changing a changes b, and sometimes not. 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. 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.
> 
> 3) ambiguous use of the form: this.that()
> 
> Sometimes, this.that() means module.funcion() as in:
> 
>>>> os.dirlist(".")
> 
> Other times, "this" is sort of like a parameter to the "that"
> function:
> 
>>>> a = list("1234")
>>>> "_".join(a)
> '1_2_3_4_5'
> 
> 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']
> 
> BTW: it seems a bit odd to that the positions of the string, and the
> delimitor, are reversed between the complementory functions join(),
> and split(). I suppose if it weren't for OO, we have something
> terribly complicated, like:
> 
> split(str, "_")
> join(str, "_")
> 
> Again, those who think in Python, will understand right away that:
> 
> math.count(x)
> 
> is counting the substring "x" in the "math" string. But can you see
> where that might be confused to be a function called count() in the
> math module?
> 
> 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. IMO: there seems to be many ambiguous, unintuitve, and
> confusing, aspects to Python.
> 

Some of your confusion is because you have ingrained ideas about how
"other" languages handle things and want Python to do it the same way.
Give it some time and you will begin to understand (as many have) that
there REALLY is method to the apparent madness...


> Some things I find odd:
>
> 1) 5/-2 == -3?
>
>

What do you except from integer arithmetic?  The ONLY possible
answers are -2, or -3.  Python chooses to always return the floor
(lower) of the two values in integer division.  While this makes
complete sense for positive integers, it seems odd for negative
ones, but it is consistent.

> With simple data types:
>>>> a = 5
>>>> b = a
>>>> a = 3
>>>> a,b
> (3, 5)
>

Here you confuse Python's complete object orientation with other
previously learned languages.

a=5

In python means: make me a name 'a' in the local namespace that points
to an object that holds a 5.  It does not mean: create a memory
area referred to by a and place a 5 in it (as many other languages
do).

b=a

In python means: make me a name 'b' in the local namespace that points
to object 'a'.

A good way to see this is to do:

id(a)
id(b)

you will see that they both point to the same object.


> Which is what I'd expect, since I have changed a, but not b.
>
> But with lists:
>>>> a = list("1234")
>>>> b = a
>>>> a.append("5")
>>>> a,b
> (['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'])
>
> b  changes even though I have not touched b. I know why, but this is
> not what I would ordinarilly expect, it does not seem intuitive. And,
> IMO, it gets worse:
>

Again this is because of objects not "containers" like Pascal or other
languages.

b=a

In python means: create me an object 'b' that points the same place as
object 'a'.

a.append("5") modifies both variables because both variables point to
the same place.

>>>> a = list("1234")
>>>> b = a
>>>> a = a + ['5']
>>>> a,b
> (['1', '2', '3', '4', '5'], ['1', '2', '3', '4'])
>
> Sometimes changing a changes b, and sometimes not. 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. 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.
>

do
>>> id(a)
18192784
>>> id(b)
18192784

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

In python means: create a new object a that is the old object a with
a '5' appended to the list.

Now look at the ids of the two variables:

>>> id(a)
12993360
>>> id(b)
18192784

See 'a' and 'b' now point to different places.  Append does in-place
append, the a+['5'] makes a new object.


> 3) ambiguous use of the form: this.that()
>
> Sometimes, this.that() means module.funcion() as in:
>
>>>> os.dirlist(".")
>
> Other times, "this" is sort of like a parameter to the "that"
> function:
>

this.that() syntax always calls the that method of the object this.

>>>> a = list("1234")
>>>> "_".join(a)
> '1_2_3_4_5'
>

"_" is a string object that has all the methods any other string object
will have.  Objects don't have to be named (via variables) to have
methods.

You could write:

underline="_"
underline.join(a)


> 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']
>

Not really, you can write:

underline="_"
b=underline.join(a)
c=b.split(underline)

seems perfectly clear to me (but I have to admit it took me a while
to see the beauty of python's syntax).

> BTW: it seems a bit odd to that the positions of the string, and the
> delimitor, are reversed between the complementory functions join(),
> and split(). I suppose if it weren't for OO, we have something
> terribly complicated, like:
>
> split(str, "_")
> join(str, "_")
>
> Again, those who think in Python, will understand right away that:
>
> math.count(x)
>
> is counting the substring "x" in the "math" string. But can you see
> where that might be confused to be a function called count() in the
> math module?

Actually it could be either.  If you write your own class called math
that has a count method this would work.  If the math module had a
count method (which it doesn't) and you import it, you might call that
method of the math module this way.  One thing you will come to
appreciate is that you can replace virtually any module, function,
etc. in Python with your own code.  A mistake many new python programmers
make is to shadow str, list, dict, etc. by using them as variable names.
Then later they can't figure out why they can't do str(n).

Hope the feedback helps.

-Larry



More information about the Python-list mailing list