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
> 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>
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':
a = "greetings from brasil!"
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()
'greetings from brasil!'
<method spam.pa of spam instance at 80e1250>
greetings from brasil!
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.
|| 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