Unbound method

jam jam at newimage.com
Tue Jun 22 23:48:47 CEST 1999


On Tue, Jun 22, 1999 at 03:31:31PM -0300, faria wrote:
> 
> Hi everyone!!!
> 
>     I'm new in Python and in Brasil we don't wave any book of Python (and
> it's very expensive to import!), so I have to ask you all this boring
> questions!
> 
>     Why it doens't work ?
> 
> --- test.py
> |
> |class A:
> |    a = 'Greetings from Brasil!!!'
> |    def pa(self):
> |        print self.a
> 
> 
> Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> from test import *
> >>> x = A
> >>> x.a
> 'Greetings from Brasil!!!'
> >>> x.pa
> <unbound method A.pa>
> >>>
> 
> 
> 

hi.

I'm going to take a stab at explaining this, since I ran into similar
confusion in some code I'm working on for Image.

what you did above was to lookup and use *unbound* methods and attributes of
the class called 'A'.

let's simplify this, and rename the class to 'spam':

class spam:
	a = "greetings from brasil!"
	def pa(self):
		print self.a

now, instantiate (or 'create') an *instance* of type class 'spam':

[~] [5:44pm] [jam at toast-pts/4] % python
Python 1.5.2 (#1, Apr 18 1999, 16:03:16)  [GCC pgcc-2.91.60 19981201 (egcs-1.1.1  on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> class spam:
...     a = "greetings from brasil!"
...     def pa(self):
...             print self.a
... 
>>> egg = spam()
>>> egg.a
'greetings from brasil!'
>>> egg.pa
<method spam.pa of spam instance at 80e1250>
>>> egg.pa()
greetings from brasil!
>>> type(egg)
<type 'instance'>
>>> type(spam)
<type 'class'>

does that help to explain what is going on a little better? I'm sorry I
don't have the precise terms used to describe these things.. what you used
in your example was a perfectly valid expression in python (it's *ok* to
reference unbound methods of objects.. I'm sure there are uses for them),
but it's probably not what you expected.

regards,
J
-- 
|| visit gfd <http://quark.newimage.com/> 
|| psa member #293 <http://www.python.org/> 
|| New Image Systems & Services, Inc. <http://www.newimage.com/>




More information about the Python-list mailing list