I have two classes, suppose A and B. Within B, I instantiate A.
I have a function func()
that is required by both the classes.
How should I go about it? I had thought of this approach:
class A:
func()
class B:
x = A()
func()
def func():
And then I can access func(开发者_C百科) from within A or B. Is this approach OK or is there a better way to do this (perhaps using an OO approach)
Note that I am new to OO programming and that is why I am interested in knowing whether I can apply any OO design to this.
Edit: The function can differ in the arguments it takes.
Define func
before you define either class, and it will be available to both.
func here can me a method of common base class
class Base(object):
def func():
#...
class A(Base):
#...
class B(Base):
#...
精彩评论