47

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 May 9, 2010 at 9:07
0

3 Answers 3

45

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'
answered May 9, 2010 at 9:13
Sign up to request clarification or add additional context in comments.

3 Comments

There is no multiple inheritance here. This is single inheritance, although it is possible that you are thinking of hierarchical inheritance.
@Nikwin, +1 as I was just going to say that.
of course, i meant just inheritance. thanks for the correction.
31

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")
answered May 9, 2010 at 10:25

2 Comments

What's the use case for your last example? I mean, if you call Alpha().sharedMethod() you get NameError: name 'x' is not defined
Is there an approach that is more pythonic than the other?
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()
answered May 9, 2010 at 9:11

1 Comment

a.k.a. mixin?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.