Mailing List Archive

Unbound method
This is a multi-part message in MIME format.

------=_NextPart_000_0014_01BEBCC4.4DF89060
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

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>
>>>



Henrique Faria
Desenvolvedor Multimídia
LAMI - Laboratório de Mídias Interativas Puc-PR
faria@ppgia.pucpr.br


------=_NextPart_000_0014_01BEBCC4.4DF89060
Content-Type: text/x-vcard;
name="Henrique Faria.vcf"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="Henrique Faria.vcf"

BEGIN:VCARD
VERSION:2.1
N:Faria;Henrique
FN:Henrique Faria
ORG:Lami
TITLE:Desenvolvedor Multim=EDdia
TEL;HOME;VOICE:330-1515 r. 2109
ADR;HOME:;;R. Guabirotuba, 137 ap. 15;Curitiba;Parana;80250-200;Brasil
LABEL;HOME;ENCODING=3DQUOTED-PRINTABLE:R. Guabirotuba, 137 ap. =
15=3D0D=3D0ACuritiba, Parana 80250-200=3D0D=3D0ABrasil
URL:
URL:http://www.lami.pucpr.br
EMAIL;PREF;INTERNET:henriquefaria@geocites.com
REV:19990622T183131Z
END:VCARD

------=_NextPart_000_0014_01BEBCC4.4DF89060--
Unbound method [ In reply to ]
faria writes:
>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>

x is a class, not an instance. This can be made clearer with
different variable names:

>>> klass = A
>>> instance = A()
>>> klass
<class __main__.A at e21d8>
>>> instance
<__main__.A instance at f22c8>
>>> klass.a
'Greetings from Brasil!!!'
>>> instance.a
'Greetings from Brasil!!!'
>>> klass.pa
<unbound method A.pa>
>>> instance.pa
<method A.pa of A instance at f22c8>

If you're expecting x = A to create an instance of the class A, that
expection is incorrect; you have to do x = A() to create an instance.

--
A.M. Kuchling http://starship.python.net/crew/amk/
In the design of fission reactors man was not an innovator but an unwitting
imitator of nature.
-- George A. Cowan, "A Natural Fission Reactor"
Unbound method [ In reply to ]
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@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/>