[英]How can I define a class in multi file(or module) in python
So I want to define a Class A
所以我想定义一个类
A
looks like: 看起来像:
in a.py
: 在
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
. 现在我想在
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? 那么,有没有简单的方法可以做到这一点?
I believe you would typically accomplish this using inheritance. 我相信您通常会使用继承来完成此任务。
You define general classes which your 'A' class inherits attributes or methods from. 您定义“ A”类从中继承属性或方法的常规类。 Something like:
就像是:
/b.py: /b.py:
class generalObjectLikeA:
def some_method_in_b_but_bellows_classA(self):
pass
/a.py: /a.py:
class A(b.generalObjectLikeA):
def __init__(self):
pass
def some_method_in_a(self):
pass
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. 如果由于某些原因(例如猴子补丁)不想使用继承,函数和类是python中的普通对象,则可以动态更改它们。
class A(object):
pass
a = A()
def bar(self):
print id(self)
A.foo = bar
a.foo()
b = A()
b.foo()
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.