Is this a bug, or is it me?

Ross Ridge rridge at caffeine.csclub.uwaterloo.ca
Fri Jan 18 16:15:43 EST 2008


Neil Cerutti <mr.cerutti at gmail.com> wrote:
>The decoration is setting the class type's f1 attribute correctly, but
>doing something strange in the local namespace.
>
>>>> class C:
>...   @staticmethod
>...   def f1(): pass
>...   print f1
>...
><staticmethod object at 0x00A69A70>
>>>> print C.f1
><function f1 at 0x00A60830>

It might help understand the problem if you do the something similar
without using @staticmethod:

	class C:
		def f1(): pass
		print "f1", f1

	print "C.f1", C.f1
	print "C().f1", C().f1

You should see output something like:

	f1 <function f1 at 0x2ac08d6377d0>
	C.f1 <unbound method C.f1>
	C().f1 <bound method C.f1 of <__main__.C instance at 0x2ac08d634cf8>>

>The class statement's local namespace is pretty strange. I think I
>mightl go back to pretending there isn't one.

When class attributes are referenced, functions are turned into unbound
methods and staticmethod objects get turned into functions.

Something like the following should work:

	class C:
		def f1(): pass
		F = {'1': f1}
		f1 = staticmethod(f1)	# if you need C.f1() to work as well

If you don't need C.f1() to work you can replace the last line with
"del f1".

					Ross Ridge

-- 
 l/  //	  Ross Ridge -- The Great HTMU
[oo][oo]  rridge at csclub.uwaterloo.ca
-()-/()/  http://www.csclub.uwaterloo.ca/~rridge/ 
 db  //	  



More information about the Python-list mailing list