In Python, I\'m trying to run a method in a class and I get an error:
Traceback (most recent call last):
File \"C:\\Users\\domenico\\Desktop\\py\\main.py\"
>>> class C:
... def f(self):
... print "hi"
...
>>> C.f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method f() must be called with C instance as
first argument (got nothing instead)
It fails because of TypeError because you didn't instantiate the class first, you have two choices: 1: either make the method static so you can run it in a static way, or 2: instantiate your class so you have an instance to grab onto, to run the method.
It looks like you want to run the method in a static way, do this:
>>> class C:
... @staticmethod
... def f():
... print "hi"
...
>>> C.f()
hi
Or, what you probably meant is to use the instantiated instance like this:
>>> class C:
... def f(self):
... print "hi"
...
>>> c1 = C()
>>> c1.f()
hi
>>> C().f()
hi
If this confuses you, ask these questions:
fibo = f.fibo
references the class itself. You probably wanted fibo = f.fibo()
(note the parentheses) to make an instance of the class, after which fibo.f()
should succeed correctly.
f.fibo.f()
fails because you are essentially calling f(self, a=0)
without supplying self
; self
is "bound" automatically when you have an instance of the class.
import swineflu
x = swineflu.fibo() # create an object `x` of class `fibo`, an instance of the class
x.f() # call the method `f()`, bound to `x`.
Here is a good tutorial to get started with classes in Python.
Differences in In python 2 and 3 version:
If you already have a default method in a class with same name and you re-declare as a same name it will appear as unbound-method call of that class instance when you wanted to instantiated it.
If you wanted class methods, but you declared them as instance methods instead.
An instance method is a method that is used when to create an instance of the class.
An example would be
def user_group(self): #This is an instance method
return "instance method returning group"
Class label method:
@classmethod
def user_group(groups): #This is an class-label method
return "class method returning group"
In python 2 and 3 version differ the class @classmethod to write in python 3 it automatically get that as a class-label method and don't need to write @classmethod I think this might help you.
Try this. For python 2.7.12 we need to define constructor or need to add self to each methods followed by defining an instance of an class called object.
import cv2
class calculator:
# def __init__(self):
def multiply(self, a, b):
x= a*b
print(x)
def subtract(self, a,b):
x = a-b
print(x)
def add(self, a,b):
x = a+b
print(x)
def div(self, a,b):
x = a/b
print(x)
calc = calculator()
calc.multiply(2,3)
calc.add(2,3)
calc.div(10,5)
calc.subtract(2,3)
In Python 2 (3 has different syntax):
What if you can't instantiate your Parent class before you need to call one of its methods?
Use super(ChildClass, self).method()
to access parent methods.
class ParentClass(object):
def method_to_call(self, arg_1):
print arg_1
class ChildClass(ParentClass):
def do_thing(self):
super(ChildClass, self).method_to_call('my arg')