1

i have a module with many functions defined in it. Is there a way to inherit all these functions into a class i have in another module.

say i have module1 with function func1(), func2()

I have another module module2 where i have a class

class class1:
    def __init__(self):
        .....
    def func3(self):
        ....

I want to inherit func1() and func2() from module1 into class1. So any object of class1 should be able to access these functions.

obj1 = class1()

I should be able to do obj1.func1()

Is there a way to acheive this in python

3
  • 1
    from module1 import class1.func3 Commented Apr 2, 2014 at 10:05
  • 1
    Functions in a module don't belong to any class, so there's nothing to inherit them from. You can import some or all of them and call them from members of the class defined in module2. Commented Apr 2, 2014 at 10:12
  • 1
    you can inherit only from a subclass. A better term to describe what you want to achieve would be to map them to the class. Since the functions in your module don't accept any parameters the question is: Do you want to map them to become static, class or instance methods? It could be achieved with metaclasses, but in the case of the latter two, the functions must accept at least one parameter which should be named cls or self respectively. Anyway, this is not a good approach. Commented Apr 2, 2014 at 10:26

3 Answers 3

1

You can import your functions from module1 to module2 and then map them from your class:

from module1 import func1, func2

class class1(object):
    ...
    def func1(self):
        return func1()

    def func2(self):
        return func2()

This is a bit strange, though. If your methods don't receive an instance of the class, why would you use them like that?

Sign up to request clarification or add additional context in comments.

Comments

1

This should do the trick.

from module1 import func1, func2

class Class1(object):

  def func3(self):
    ...

setattr(Class1, 'func1', func1)
setattr(Class1, 'func2', func2)

Be careful when you define func1 and func2 to add self as first argument.

Comments

0

If you only want to include a few functions from a module - and don't require them to be passed the instance when called - assign them as static methods to the class:

from module1 import func1, func2

class Class1(object):
    func1 = staticmethod(func1)
    func2 = staticmethod(func2)

If you want to include all of the functions, you can override __getattr__:

import module1

class Class1(object):
    def __getattr_(self, attr):
        try:
            return getattr(module1, attr)
        except AttributeError:
            # catch & re-raise with the class as the 
            # object type in the exception message
            raise AttributeError, 'Class1' object has no attribute '{}'.format(attr)

Comments

Your Answer

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.