AttributeError - a dumb question
Johann Spies
jhspies at futurenet.co.za
Tue Jul 6 12:17:28 EDT 1999
Hallo Tres,
Thanks for your reply.
On Tue, 6 Jul 1999, you wrote:
> In article <Pine.LNX.3.96.990706171320.374A-100000 at Johann> you write:
> >Why does the following code produce an attribute error?
> >class F:
> > def __init__(self):
> >
> > x = self.Krylys()
>
> If you want 'x' to be an attribute of the current instance, you
> need to say, 'self.x = self.Krylys()' here; otherwise, you are
> assigning to a local variable of the __init__ method.
I have tried that. Here is the new code with output (sorry for the type
in the previous example:
class F:
def __init__(self):
self.x = self.Krylys()
def KryLys(self):
l = [1,2,3,4,5]
return l
l = F
print dir(l)
print hasattr(l, 'x')
print hasattr(l,'Krylys')
print l.x
output:
['KryLys', '__doc__', '__init__', '__module__']
0
0
Traceback (innermost last):
File "<stdin>", line 15, in ?
AttributeError: x
> Because 'Krylys' <> 'Krypos'?
Stupid typing error on my side.
Johann
--------------------------------------------------------------------------
| Johann Spies Windsorlaan 19 |
| jhspies at futurenet.co.za 3201 Pietermaritzburg |
| Tel/Faks Nr. +27 331-46-1310 Suid-Afrika (South Africa) |
--------------------------------------------------------------------------
"Ask, and it shall be given you; seek, and ye shall
find; knock, and it shall be opened unto you."
Matthew 7:7
More information about the Python-list
mailing list