-3

I have written some python code:

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. I don't know why I am getting this error. Is it because of 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.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63

3 Answers3

1

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.

You don't have to use object.__init__(self, name, age) here. If you remove that line and implement the changes above, your code will work just fine.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
  • can you tell me the use of object.__init__(self, name, age)...in which situation we use it – user3517846 Apr 12 '14 at 15:35
  • Maybe [this explanation](https://stackoverflow.com/questions/625083/python-init-and-self-what-do-they-do) will be of help to you. – Stefan van den Akker Apr 12 '14 at 16:25
  • so its not necessary to use object.__init__(self, name, age) ..right ? – user3517846 Apr 12 '14 at 16:39
  • No, that's not necessary. I don't understand why you would want to initialize `object` in the first place. Class `Key` inherits from `object` (all classes do), but there is no need to initialize `object`. `object` is what classes come from, it is the most base type of thing. When a class inherits from `object` it means it just inherits the standard special methods like `__init__`, `__doc__`, `__str__`, etc. I have yet to encounter an example where `object` is initialized the way you say, so please post the original code. – Stefan van den Akker Apr 12 '14 at 17:00
0

When you do baby = key('radar', 20) you are actually passing three arguments: the instance, the name and the age. However your initialiser is defined to take exactly two arguments so you get a TypeError.

self is the argument implicitly passed when referring to an instance of an object.

For your __init__ function, I would just do:

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.

It makes no sense here to call object.__init__ at all, just remove that line.


Apart from that, everything works fine (except use %s instead of %d).

Testing:

>>> baby = key('radan', 20)
>>> baby.somefunction()
yay the name is radan
anon582847382
  • 19,907
  • 5
  • 54
  • 57
0

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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
laike9m
  • 18,344
  • 20
  • 107
  • 140
  • can you tell me the use of object.__init__(self, name, age)...in which situation we use it ? – user3517846 Apr 12 '14 at 15:37
  • @user3517846 If your class is directly inherited from `object`, then you'll never need to call `object.__init__()` – laike9m Apr 12 '14 at 15:39
  • can you give me a link or something or can you please explain more its use ..in some code i have seen using object.__init__(self) ..i didnt understand its usage ..Please help me – user3517846 Apr 12 '14 at 15:43
  • @user3517846 In what code have you seen `object.__init__(self)`? – laike9m Apr 12 '14 at 15:44
  • i dunno am not so clear but i have seen objec.__init__(self,name) like that ...Can you please tell me why its using like that – user3517846 Apr 12 '14 at 15:47
  • I have accepted your answer ..please tell me the correct situation to use object.__init__(self,name) – user3517846 Apr 12 '14 at 15:54
  • @user3517846 ok give me some time to search cause for me I've never used or seen code written like that. If I find some useful information I'll tell you. – laike9m Apr 12 '14 at 15:58
  • @user3517846 I'll comment here – laike9m Apr 12 '14 at 16:07
  • will be waiting for your comment – user3517846 Apr 12 '14 at 16:16
  • 1
    @user3517846 After searching for a while, my answer is still "NO, there is no such usage". I think what you saw is that people are using the word `object` to refer to general class instances, for instance you'll find it in the official doc: https://docs.python.org/3/reference/datamodel.html#object.__init__, but that doesn't mean to write `object.__init__(self...)`, it means object is an instance whatever type it is. my email is laike9m@gmail.com in case you want to know more. – laike9m Apr 12 '14 at 16:50
  • I understood and can you tell me why we use the word object with the classname ? – user3517846 Apr 13 '14 at 03:16
  • @user3517846 Because `__init__` is instance method not clas method, please refer to http://stackoverflow.com/questions/17134653/difference-between-class-and-instance-methods. – laike9m Apr 13 '14 at 05:28