Newbie look at Python and OO

walterbyrd walterbyrd at iname.com
Thu May 10 11:58:54 EDT 2007


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.




More information about the Python-list mailing list