Python 使用super从方法传递参数
我试图在一个名为getphone的方法下设置参数role='r'。它在init下使用super运行正常,但我不知道如何在另一种方法下运行 角色是为正在运行的api设置权限级别 此代码正在运行Python 使用super从方法传递参数,python,oop,superclass,Python,Oop,Superclass,我试图在一个名为getphone的方法下设置参数role='r'。它在init下使用super运行正常,但我不知道如何在另一种方法下运行 角色是为正在运行的api设置权限级别 此代码正在运行 PATH = 'home_drive_' PLATFORM = 'Linux_' ITEM = '_PC' class Credential: def __init__(self, *, path, platform, role='', **kwargs): super().__i
PATH = 'home_drive_'
PLATFORM = 'Linux_'
ITEM = '_PC'
class Credential:
def __init__(self, *, path, platform, role='', **kwargs):
super().__init__(**kwargs)
self.role = role
self.username_file = path + platform + role
class AXL(Credential):
def __init__(self, *, item, **kwargs):
super().__init__(role='rw', **kwargs)
self.item = item
def getphone(self):
self.role = 'r'
return self.username_file + self.item
def writephone(self):
self.role = 'rw'
return self.username_file + self.item
def statusphone(self):
self.role = 'rwx'
return self.username_file + self.item
reg1 = AXL(path=PATH, platform=PLATFORM, item=ITEM)
print(reg1.getphone())
print(reg1.writephone())
print(reg1.statusphone())
在类AXL下,我想将角色='r'移动到方法getphone下
我已经试过了,但我不明白为什么我需要设置路径和平台
PATH = 'home_drive_'
PLATFORM = 'Linux_'
ITEM = '_PC'
class Credential:
def __init__(self, *, path, platform, role='', **kwargs):
super().__init__(**kwargs)
self.role = role
self.username_file = path + platform + role
class AXL(Credential):
def __init__(self, *, item, **kwargs):
super().__init__(**kwargs)
self.item = item
def getphone(self):
super().__init__(path=PATH, platform=PLATFORM, role='r')
return self.username_file + self.item
def writephone(self):
super().__init__(path=PATH, platform=PLATFORM, role='rw')
return self.username_file + self.item
def statusphone(self):
super().__init__(path=PATH, platform=PLATFORM, role='rwx')
return self.username_file + self.item
reg1 = AXL(path=PATH, platform=PLATFORM, item=ITEM)
print(reg1.getphone())
print(reg1.writephone())
print(reg1.statusphone())
可以找到沙盒没有一个角色与实例关联;相反,角色与实例调用的方法相关联。试试这个
PATH = 'home_drive_'
PLATFORM = 'Linux_'
ITEM = '_PC'
class Credential:
def __init__(self, *, path, platform, **kwargs):
super().__init__(**kwargs)
self.username_file = path + platform
class AXL(Credential):
def __init__(self, *, item, **kwargs):
super().__init__(**kwargs)
self.item = item
# "Private" method used to implement the other phone methods
# You could inline this if you want.
def _phone(self, role):
return self.username_file + role + self.item
def getphone(self):
return self._phone('r')
def writephone(self):
return self._phone('rw')
def statusphone(self):
return self._phone('rwx')
reg1 = AXL(path=PATH, platform=PLATFORM, item=ITEM)
print(reg1.getphone())
print(reg1.writephone())
print(reg1.statusphone())
super()
将允许您访问父类范围。在python3中,您可以以super().method(args)
的形式从它访问任何方法,而在Python2中,格式是super(YourClass,self).method(args)
从OOP的角度来看,在每个方法的父级上调用
\uuuuu init\uuuu
。您正在父对象的生存期内重新初始化它。如果要从父级设置角色,只需使用self.role=…
。父级访问self.role
的任何方法都会看到修改。如果我理解正确,您可以在AXL.final
中设置self.role='r'
。如果我理解正确,您就不能(至少不能以任何符合约定的方式进行设置,并且很容易调试)<代码>角色是初始化凭证
实例所必需的,因此在初始化AXL
实例时必须提供该角色。不清楚您的最终目标是什么。是否要用调用时使用给定角色计算值的方法替换属性用户名\u文件
(当前在\uuuuu init\uuuu
中初始化)?(将角色
从实例的属性转换为最终
的参数)例如,我将使用AXL.getphone、AXL.writephone和AXL.statusphone。getphone的角色为只读(r)写Phone的角色为读/写(rw)状态Phone的角色为读/写/执行(rwx)根据角色的不同,凭据将返回用户名/pwd以供使用you@chepner,这就是我正在尝试做的,正在工作,但不确定如何在我复制的真实脚本中使用它。它看起来像是任何涉及角色的东西,应该是一种使用角色片段动态构建路径的方法,而不是作为实例的属性。或者,你应该有更多的子类,每个子类都有一个固定的角色。嗨,像这样做怎么样。谢谢