Reputation: 11
Hello I'm learning python, it is also my first language. I'm trying to figure out how classes work. I have this small code and I can't get it to work after a week of searching. Thank you for helping me.
Also I'm trying to figure out what getattr and super do. I read on the documentation but its not easy for me to understand. English is not my native language and its a little difficult to understand sometimes.If you can explain this two things or if you know any website that explains it in a simple way I would really thank you for it.
here is the code:
import sys
class Map(object):
dicti = {'stuff': stuff(),
'more_stuff': more_stuff()
}
class Stuff:
def stuff(self):
print "did it work?"
next = raw_input("type next: ")
if next == "next":
return 'more_stuff'
else:
print "what? lets try again."
return 'stuff'
class MoreStuff:
def more_stuff(self):
print "ok"
next = raw_input('type next: ')
if next == "next":
return 'stuff'
else:
print "bye."
sys.exit(0)
class NewClass(Map):
def __init__(self, themap):
self.themap = themap
def Continu(self):
next = self.themap
while True:
print "----"
room = next()
a_test = NewClass('stuff')
a_test.Continu()
Upvotes: 0
Views: 347
Reputation: 3524
The problem with your class is that it doesn't seem to represent anything (sorry if that's harsh). Rather than fix your code, I'll just show you a couple examples.
These are example classes that I wrote. The first one is kind of ridiculous, but actually simple and could teach you something. It uses hasattr
which is not a far stretch from getattr
.
You're classes can have logic in them, but try to see them as ways of managing information about an object. I mean object in both the python sense and in the normal meaning of the word.
Also, I noticed you have some other classes indented under Map
. Just work with classes as separate things for now. Indenting those like that does not give them any special relationship to Map
class OppMan:
def __init__(self, awake = False):
self.truth = awake
self.answers = 0
def echo(self, userIn):
if type(userIn) == (int or float):
answer = -1*self.truth*userIn
elif hasattr(userIn, '__str__') or hasattr(userIn, '__repr__'):
answer = 'not '*self.truth + str(userIn)
else:
try:
answer = -1*userIn
except:
answer = 'I am only human'
self.answers += 1
return answer
Example usage:
Barney = OppMan(True)
print(Barney.echo(420))
print(Barney.echo('yours'))
print(Barney.echo(['a','b','c']))
print(Barney.answers)
Betty = OppMan()
print(Betty.echo('you figure it out'))
This one is like fractions, sort of:
class Rational:
def __init__(self, numer, denom):
self.n = numer
self.d = denom
def __add__(self, other):
return Rational((self.n*other.d)+(self.d*other.n), self.d*other.d)
def __mul__(self, other):
return Rational(self.n*other.n, self.d*other.d)
def __str__(self):
return str(self.n) + '/' + str(self.d)
Example usage:
foo = Rational(3,5)
bar = Rational(1,7)
print(foo+bar)
foo = Rational('3','5')
bar = Rational(1,2)
print(foo+bar)
I'm not sure what you're trying to do with the "room", dictionary, etc., but study these and try to find some simple problems to solve. It might take a while to figure out how these work, but you'll know more about classes in the end.
Upvotes: 0
Reputation: 304175
You are passing the string 'stuff'
into NewClass
, so that is becoming self.themap
later you are assigning next = self.themap
, so now next
is also a reference to the string 'stuff'
.
room = next()
will fail because you can't call a string
Upvotes: 1