Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 17 Online
OpenStudy (anonymous):

Can someone help me with this code? I keep getting an unbound method. class database(object): def __init__(self): self.l = [] def insert(self, e): for i in range[len(self.l)]: if i == e: return self.l.append(e)

OpenStudy (anonymous):

please use a code pasting site like dpaste.com or pastebin.com, ...... is that all of your code - you only posted a class definition. that is not the code/statement that results in an unbound method message. http://www.google.com/search?q=python+unbound+method&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&safe=active the short answer would be that you need to create an instance of the class foo = database then call the insert method with the instance foo.insert('blah') instead of a for loop your insert function/method should just say if e in self.I: return

OpenStudy (anonymous):

This is the code http://dpaste.com/1197246/ if you run it says something about an unbound method( which I fail to understand).

OpenStudy (anonymous):

x = database just assigns the class to x http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables to create an instance - foo = database() - http://dpaste.com/1197706/ again the short answer is that insert() is an instance method. the self in the arguments requires that an instance of the class be passed to it. when you try to call the method with the class itself, you get the TypeError. There is information in the Python documentation and there are some particularly good videos at http://pyvideo.org/. unfortunately I cannot point you to a specific place for the knowledge - if I recall, I just stumbled on it and it only became crystal clear to me after a lot of reading and watching videos of Python talks. If I run across something specific - i'll post it. best would be searches for phrase like: class methods, instance methods, class construction, unbound methods, __new__, include python in the seacrhes

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!