If I have two classes, and one of them has a function that I want to use in my other class, what do I use so that I don't have to rewrite my function?
Asked
Active
Viewed 7.0k times
3 Answers
44
There are two options:
- instanciate an object in your class, then call the desired method on it
- use @classmethod to turn a function into a class method
Example:
class A(object):
def a1(self):
""" This is an instance method. """
print "Hello from an instance of A"
@classmethod
def a2(cls):
""" This a classmethod. """
print "Hello from class A"
class B(object):
def b1(self):
print A().a1() # => prints 'Hello from an instance of A'
print A.a2() # => 'Hello from class A'
Or use inheritance, if appropriate:
class A(object):
def a1(self):
print "Hello from Superclass"
class B(A):
pass
B().a1() # => prints 'Hello from Superclass'

miku
- 181,842
- 47
- 306
- 310
-
4There is no multiple inheritance here. This is single inheritance, although it is possible that you are thinking of hierarchical inheritance. – Nikwin May 09 '10 at 09:32
-
@Nikwin, +1 as I was just going to say that. – Carson Myers May 09 '10 at 10:06
-
of course, i meant just inheritance. thanks for the correction. – miku May 09 '10 at 10:07
29
There are several approaches:
- Inheritance
- Delegation
- Super-sneaky delegation
The following examples use each for sharing a function that prints a member.
Inheritance
class Common(object):
def __init__(self,x):
self.x = x
def sharedMethod(self):
print self.x
class Alpha(Common):
def __init__(self):
Common.__init__(self,"Alpha")
class Bravo(Common):
def __init__(self):
Common.__init__(self,"Bravo")
Delegation
class Common(object):
def __init__(self,x):
self.x = x
def sharedMethod(self):
print self.x
class Alpha(object):
def __init__(self):
self.common = Common("Alpha")
def sharedMethod(self):
self.common.sharedMethod()
class Bravo(object):
def __init__(self):
self.common = Common("Bravo")
def sharedMethod(self):
self.common.sharedMethod()
Super-sneaky Delegation
This solution is based off of the fact that there is nothing special about Python member functions; you can use any function or callable object so long as the first parameter is interpreted as the instance of the class.
def commonPrint(self):
print self.x
class Alpha(object):
def __init__(self):
self.x = "Alpha"
sharedMethod = commonPrint
class Bravo(object):
def __init__(self):
self.x = "Bravo"
sharedMethod = commonPrint
Or, a similarly sneaky way of achieving delegation is to use a callable object:
class Printable(object):
def __init__(self,x):
self.x = x
def __call__(self):
print self.x
class Alpha(object):
def __init__(self):
self.sharedMethod = Printable("Alpha")
class Bravo(object):
def __init__(self):
self.sharedMethod = Printable("Bravo")

Michael Aaron Safyan
- 93,612
- 16
- 138
- 200
-
What's the use case for your last example? I mean, if you call `Alpha().sharedMethod()` you get `NameError: name 'x' is not defined` – stelios Dec 04 '18 at 08:58
-
7
you create a class from which both classes inherit.
There is multiple inheritance, so if they already have a parent it's not a problem.
class master ():
def stuff (self):
pass
class first (master):
pass
class second (master):
pass
ichi=first()
ni=second()
ichi.stuff()
ni.stuff()

o0'.
- 11,739
- 19
- 60
- 87