Reputation: 155
I am learning Python. I am getting this error when I attempt to call a function which is inside the class. So please help me to fix this.
tony@kali:~$ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class H:
... def y():
... print "j"
...
>>> g=H
>>> g.y()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method y() must be called with H instance as first argument (got nothing instead)
>>> H.y()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method y() must be called with H instance as first argument (got nothing instead)
>>>
Upvotes: 0
Views: 119
Reputation: 11322
You've got answers and comments, so let me explain the error message.
In your code, g
and H
are different names for the same thing, so the two errors are one.
Now, H
is an (old-style) class and all the functions def
'ed below it, unless "processed" with tricks like classmethod
or staticmethod
, are methods. When you access the methods as attributes of the class H
, they're known as "unbound" methods because they're not yet bound to an instance of the class H
.
To understand how Python's core model of classes, methods, and attributes, I recommend the excellent posts by Shalabh Chaturvedi:
The tl;dr version: normally you're expected to call a method by invoking an instance's bound method:
>>> class H(...):
>>> def y(...):
>>> h_instance = H()
>>> h_instance.y(...)
Python will put that instance (h_instance
here in the fake code snippet) as the first argument to the method. If you call it as h_instance.y()
, the actual argument list is (h_instance)
; if you call it as h_instance.y(a)
, the arguments are (h_instance, a)
.
This is why you almost invariably find the first formal argument in a method definition to be self
. There's nothing special about this name, except that Python expects that slot be reserved for the instance on which the method is invoked. It allows you to define the method with an empty argument list, but that one wouldn't be useful when you actually use the instance. In that case you'll always get TypeError
-- because the call sends ONE argument (the "self
" instance) while there's none in the method definition.
As a demonstration, consider the following session:
In [1]: class H(object): # New-style class, doesn't matter here.
...: def y(self): # Notice the call signature: "self" included
...: print "j"
In [2]: h_instance = H() # Create an instance from the class
# This is how normally you're expected to call. There's no argument
# passed to the *bound* to the instance h_instance, because when you do
# this Python will carry out the syntactic desugaring and call on the
# instance.
In [3]: h_instance.y()
j
# This does the same thing and demonstrates the concept of an
# unbound method. It's not bound to any specific instance of H, so
# you'll have to specify which instance to call.
In [4]: H.y(h_instance)
j
Upvotes: 0
Reputation: 2172
You have to initialize the class
, then only you can call the methods of that class
. Also, methods should have it's first argument as self
.
class H:
def y(self):
print "j"
g = H()
g.y()
Refer here for more details about self.
You should definitely check this chapter for class.
Upvotes: 1