[英]Inner class in python without affecting the normal operation of the program
I'm learning Python at the same time I'm working on a subject project, and I realized that I have two classes (class senA and senB) that represent two similar elements, therefore, I wondered if those two classes could be grouped in a general class (General class), I was looking at the inner class but unfortunately as I must access the arguments of each class (senA and senB) always generates an error.我正在学习 Python,同时我正在研究一个主题项目,我意识到我有两个类(senA 类和 senB 类)代表两个相似的元素,因此,我想知道这两个类是否可以分组a general class (General class), I was looking at the inner class but unfortunately as I must access the arguments of each class (senA and senB) always generates an error. My code is like this
我的代码是这样的
class senA():
def __init__(self, arg, arg2):
self.arg = arg
self.arg2 = arg2
class sensB():
def __init__(self, arg3):
self.arg3 = arg3
for i in range (3):
obj = senA('hx', 'ex')
objx = sensB ('hello')
values=list(attrs.values())
valuesx=list(attrsx.values())
list_VALORES.append(values)
list_metodos.append(valuesx)
print(list_metodos)
print(list_VALORES)
and it works well but I would like to associate the classes senA and senB into one called classGeneral它运作良好,但我想将 senA 和 senB 类关联到一个名为 classGeneral 的类中
class general():
class senA():
def __init__(self, arg, arg2):
self.arg = arg
self.arg2 = arg2
class sensB():
def __init__(self, arg3):
self.arg3 = arg3
but it generates constant errors or for example the program stops recognizing the classes when I instantiate the objects.但它会产生持续的错误,或者例如当我实例化对象时程序停止识别类。 Any idea how I could solve this
知道如何解决这个问题
Sorry about my bad English, but I'm not a native English speaker.对不起我的英语不好,但我不是以英语为母语的人。 I try to be as clear as possible
我尽量说清楚
From what I understand, what you want is to create a separate module to hold the senA
and sensB
classes.据我了解,您想要的是创建一个单独的模块来保存
senA
和sensB
类。 If you create a python file called general.py
that holds the class definitions, you can then import that into another python script.如果您创建一个名为
general.py
的 python 文件,其中包含 class 定义,则可以将其导入另一个 python 脚本。 So in your main.py
, you would add the following line to the top: from general import senA, sensB
, and you would be able to use them like normal.因此,在您的
main.py
中,您可以在顶部添加以下行: from general import senA, sensB
,您就可以像往常一样使用它们。
If your classes are all similar and share attributes, than you could also use inheritance.如果你的类都是相似的并且共享属性,那么你也可以使用 inheritance。 For example:
例如:
class General:
def __init__(self, arg1):
self.arg1 = arg1 # argument that both senA and sensB use
class senA(General):
def __init__(self, arg1, arg2):
super().__init__(arg1)
self.arg2 = arg2
class sensB(General):
def __init__(self, arg1, arg2, arg3):
super().__init__(arg1)
self.arg2 = arg2
self.arg3 = arg3
You could then access attributes from those classes as normal, for example:然后,您可以正常访问这些类的属性,例如:
a = senA('arg1 value', 'arg2 value')
print(a.arg1) # returns 'arg1 value'
print(a.arg2) # returns 'arg2 value'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.