[Tutor] Uncallable class methods?

Patrick K. O'Brien pobrien@orbtech.com
Tue, 12 Jun 2001 00:01:04 -0500


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

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


>>> dir(x)
['__doc__', '__module__', 'y']
>>> x.y
<unbound method x.y>
>>> x.y()
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in ?
    x.y()
TypeError: unbound method y() must be called with instance as first argument
>>> z = x()
>>> z
<__main__.x instance at 016BDC1C>
>>> z.y()
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in ?
    z.y()
TypeError: y() takes no arguments (1 given)
>>>

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.
---
Patrick K. O'Brien
Orbtech
"I am, therefore I think."

-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
Allan Crooks
Sent: Monday, June 11, 2001 11:23 PM
To: tutor@python.org
Subject: [Tutor] Uncallable class methods?

Hi,

Quick question about the following piece of code:

>>> class X:
..   def y():
..     print 'I am y of x.'

Is there anyway that y can be invoked? My understanding is no, in which
case, can anyone tell me why the interpreter doesn't complain about a class
method being declared which does not have the minimum of one argument?

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

Allan.


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor