[Tutor] Uncallable class methods?

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 11 Jun 2001 22:27:55 -0700 (PDT)


On Tue, 12 Jun 2001, Patrick K. O'Brien wrote:

> Hmmm. Very interesting question. You would think that something would
> complain sooner. Here is what I got:

I suspect it has to do with the way Python does things dynamically.  For
example, typing out the following:

###
def test1():
   foobar
###

doesn't break, at least as long as we don't actually call test1().  But
when we actually do call test1(), that's when Python looks inside and sees
that 'foobar' hasn't been defined.

Likewise, when we're defining a class's members, like the example:

> >>> class x:
> 	def y():
> 		print 'Yo'


Python doesn't call y() yet, and so it doesn't detect the error about the
missing "self" argument.

As a wild guess, I think Python does it this way to reduce the number of
rules it needs to check as it reads our definitions.  When we're defining
"members" of a class, we're using the framework for defining regular
functions, with the small rule that the first argument will magically get
filled in with an instance... but the procedure that reads def's doesn't
really need to know about the special role of 'self'.

I believe that the PyChecker utility here:

    http://pychecker.sourceforge.net

does check for the kind of bugs like this.


> Kind of a funny catch-22 going on. For the total newbies, def y() should
> have been declared as def y(self), of which Allan is well aware. The point
> is why does python let us mess up so badly in the first place? And I do not
> know the answer to that question.

It's probably to make the Python implementors lives a little easier: they
can reuse the same kind of definition-reading code without having to
special-case it for class definitions.  The consequence, unfortunately, is
that it isn't quite as helpful as we'd like it; the parser isn't context
senstive.

But perhaps this can be fixed!  You might want to raise this question to
them; this definitely sounds like the sort of thing that catches people
off guard.


> Of course, considering the wealth of replies I received about my query about
> buffers, maybe I'm asking in the wrong place. :)

*head in shame*