[Tutor] Proper uses of classes
frank ernest
doark at mail.com
Wed Apr 3 21:43:02 CEST 2013
Hi guys, it's been a while since I posted and I've learned a lot since then. Today I have a question on classes, I can't get mine to work.
class alist(list):
def __init__(self, b, a):
self = list()
self.append(b)
a = a + b
def appendit(self):
self.append(a)
print(alist(2,4))
[]
#It's blank!
c = alist(2,4)
c.appendit()
print(c)
[[...]]
#It's still blank!
If I add this:
a = a + b
the last line of my deffinition I get:
c = alist(2,4)
c.appendit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in appendit
UnboundLocalError: local variable 'a' referenced before assignment
If I make a nonlocal I get
SyntaxError: name 'a' is parameter and nonlocal
I want it to get my list and all the members in it when printing for instance. I also would like to without making them global create two variables which I can use throughout the whole class as their value will not change.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130403/fbe79330/attachment.html>
More information about the Tutor
mailing list