简体   繁体   English

python中带有__init__的对象

[英]Object with __init__ in python

I have written some python code:我写了一些python代码:

class key(object):
    def __init__(self,name):
        object.__init__(self,age)
        this.name = name
        this.age = age

    def somefunction(self):
        print "yay the name is %d" % self.name

baby = key('radan',20)
baby.somefunction()

When I create an instance of key with baby = key('radan',20) , I got a TypeError.当我用baby = key('radan',20)创建一个key实例时,我得到了一个类型错误。 I don't know why I am getting this error.我不知道为什么我会收到此错误。 Is it because of object.__init__(self,age) ?是因为object.__init__(self,age)吗?

If yes, please help me in explaining why we use object.__init__(self,age) and what the purpose of that is and help me solve this code.如果是,请帮助我解释为什么我们使用object.__init__(self,age)以及它的目的是什么,并帮助我解决此代码。

Some pointers:一些提示:

class Key(object): # in Python, classes are usually named with a starting capital

    def __init__(self, name, age): # name all your arguments beside self

        self.name = name # use self.name, not this.name
        self.age = age

    def somefunction(self):

        print "yay the name is %s" % self.name

baby = Key('radan',20)
baby.somefunction()
# output: yay the name is radan

Actually, you can can name the self instance parameter whatever you like in Python (even this ), but it makes the code harder to read for other people, so just use self .实际上,您可以在 Python 中随意命名self实例参数(甚至是this ),但这会使其他人难以阅读代码,因此只需使用self

You don't have to use object.__init__(self, name, age) here.您不必在这里使用object.__init__(self, name, age) If you remove that line and implement the changes above, your code will work just fine.如果您删除该行并实施上述更改,您的代码将正常工作。

When you do baby = key('radar', 20) you are actually passing three arguments: the instance, the name and the age .当您执行baby = key('radar', 20)您实际上是在传递三个参数:实例、 nameage However your initialiser is defined to take exactly two arguments so you get a TypeError .但是,您的初始化程序被定义为正好采用两个参数,因此您会得到TypeError

self is the argument implicitly passed when referring to an instance of an object. self是引用对象实例时隐式传递的参数。

For your __init__ function, I would just do:对于您的__init__函数,我会这样做:

def __init__(self, name, age):
    self.name = name
    self.age = age

So we can assign the arguments passed as attributes to the current instance, conventionally called self .因此,我们可以将作为属性传递的参数分配给当前实例,通常称为self

It makes no sense here to call object.__init__ at all, just remove that line.在这里调用object.__init__完全没有意义,只需删除该行即可。


Apart from that, everything works fine (except use %s instead of %d ).除此之外,一切正常(除了使用%s而不是%d )。

Testing:测试:

>>> baby = key('radan', 20)
>>> baby.somefunction()
yay the name is radan

Your code contains several errors:您的代码包含几个错误:

class key(object):
    def __init__(self, name, age): # where's your age?
        self.name = name  # no need to call object.__init__
        self.age = age    # there is no such thing called "this", use self

    def somefunction(self):
        print "yay the name is %s" % self.name  # use %s as the comment says

baby = key('radan', 20)
baby.somefunction()

output:输出:

>>> baby = key('radan', 20)
>>> baby.somefunction()
yay the name is radan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM