Another try at Python's selfishness

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Feb 3 07:55:18 EST 2006


On Fri, 03 Feb 2006 03:59:10 -0800, n.estner wrote:

>> I still see "newbie-friendliness" as a
>> MAJOR plus for Python -- it increases the chance that users
>> of your software will become contributors.
> 
> Yes, I 100% agree to that point!
> But the point is, the current situation is not newbie-friendly (I can
> tell, I am a newbie): I declare a method with 3 parameters but when I
> call it I only pass 2 parameters. That's confusing.

Yes, I understand what you mean now. When I was a newbie, it took me a
little while to get the difference between ordinary functions and methods
too, although I had little OO experience before Python. I don't know if
that helped or hindered the learning experience.

With the current syntax, you have to learn some special behaviour:

class Foo:
    def bar(self, x, y):
        pass

but you call Foo().bar(x, y) 

But with your syntax, you still have to learn special behaviour:


# outside the class definition
self.bar  # look up "bar" in self's namespace. self must already exist.

# inside a class definition
class Foo:

    self = Something()  # normal class attribute "self" created
    # this works because self is not a keyword
    # Foo now has an attribute "self"

    def self.bar(x, y):
        # create a method "bar" with "self" in bar's namespace
        # Foo now has an attribute "bar" (which is a method)
        pass

    self.bar = None   # normal attribute access
    # Foo attribute "self" is modified to have an attribute "bar"

    # there is no conflict between self the attribute and self the 
    # special parameter because they live in different namespaces
    # but looking at them, there is an apparent conflict


So, most of the time, foo.bar looks up attribute "bar" in foo; but in a
method definition, foo.bar assigns attribute "foo" in bar. That's special
behaviour too, and it seems to me probably harder to live with and even
more confusing than the behaviour you aim to remove.



-- 
Steven.




More information about the Python-list mailing list