开发者

Beginner having a problem with classes

I'm working through O'Reilly's "Learning Python" and having a problem with classes. I think I understand the concept, but in practice have stumbled upon this problem. Fron page 88-89:

>>> class Worker:
def __innit__(self, name, pay):
    self.name=name
    self.pay=pay
    def lastName(self):
        return self.name.split()[-1]
    def giveRaise(self, percent):
        self.pay*=(1.0+percent)

Then the book says "Calling the class like a function generates instances of a new type ...etc" and gives this example.

bob = Worker('Bob Smith', 50000)

This gives me this error:

TypeError: this constructor takes no arguments.

And then I start muttering 开发者_如何学运维profanities. So what am I doing wrong here?

Thanks for the help.


David, you've mis-typed __init__

The correct spelling is this:

def __init__(self, name, pay):
    self.name=name
    self.pay=pay
def lastName(self):
    return self.name.split()[-1]
def giveRaise(self, percent):
    self.pay*=(1.0+percent)


One misspelling: it's __init__, not __innit__ -- just one n.

You also have some indentation problems but I think those may be due to just copy-and-paste issues, or else you'd be getting SyntaxErrors before the TypeError you relate;-).

So your code should probably be:

class Worker(object):
    def __init__(self, name, pay):
        self.name=name
        self.pay=pay
    def lastName(self):
        return self.name.split()[-1]
    def giveRaise(self, percent):
        self.pay*=(1.0+percent)

I've also added the (object) to class Worker -- in Python 3.* it doesn't matter, but in 2.* it does (and even in 3.* it doesn't hurt, anyway;-). This makes the class "new style", which doesn't matter at very elementary levels but will as soon as you start wanting to do anything "cool and interesting" such as adding properties;-).


You wrote __innit__ instead of __init__.

In this way, you declared just another method in the class, not the special method called the constructor, which is by definition named __init__. This causes Python to generate a default constructor taking no arguments, hence the error message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜