0

So I want to define a Class A
looks like:

in a.py:

class A:
   def __init__(self):
       pass

   def some_method_in_a(self):
       pass

and now I want define some additional methods in b.py. looks like:

continue define class A:
def some_method_in_b_but_bellows_classA(self):
    pass

So are there any simple methods to do this?

2
  • What does 'continue define' means here? Adding some new methods to the old class? Overriding some attributes? Commented Oct 16, 2013 at 15:46
  • Why not define them in sub classes? Commented Oct 16, 2013 at 16:00

2 Answers 2

2

I believe you would typically accomplish this using inheritance.

You define general classes which your 'A' class inherits attributes or methods from. Something like:

/b.py:

class generalObjectLikeA:
    def some_method_in_b_but_bellows_classA(self):
        pass

/a.py:

class A(b.generalObjectLikeA):
    def __init__(self):
        pass

    def some_method_in_a(self):
        pass
Sign up to request clarification or add additional context in comments.

4 Comments

as a side note: camelCase is considered 'unpythonic', but I like it.
camelCase is only unpythonic for variable and function/method names. It's the recommended convention for class names.
@chepner CamelCase is not camelCase
Good point. I glossed over the (lack of a) initial capitalization.
1

If you don't want to use inheritance for some reasons(like monkey patch), functions and classes are normal objects in python, you can change them dynamiclly.

class A(object):
    pass

a = A()

def bar(self):
    print id(self)

A.foo = bar
a.foo()
b = A()
b.foo()

References:

Monkey patch

Monkeypatching For Humans

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.