A Novice Class Question.

Erik Max Francis max at alcyone.com
Wed Aug 28 02:37:40 CEST 2002


Mauro wrote:

> When I run, I type this: Conjuntos.Conjs.NewList([0,1], [2,3])
> This is the message of error:
> 
> TypeError: unbound method NewList() must be called with Conjs instance
> as first argument (got list instance instead)

It looks like you want the equivalent of a static method in other
languages.  All functions defined directly inside of a class declaration
are instance methods, and so must take a self argument (although it can
be named something other than `self', though this isn't terribly
recommended).

In later versions of Python you can get the desired behavior you're
seeking with the staticmethod wrapper:

	class C:
	    def f(a, b): print a, b
	    f = staticmethod(f)

Now you can call f either totally without reference to an instance
C.f(...), or tacked onto an existing instance c = C(); c.f(...) and both
will work as you expect.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list