I'm trying to set the self.role under credential class to use the self.role under AXL class. the idea is to have different classes based on the role the api needs to be. if axl class is only reading data then the role would be r.
PATH = 'home_drive_'
PLATFORM = 'Linux_'
ITEM = '_PC'
class Credential:
def __init__(self, path, platform):
self.role = 'rx'
self.username_file = path + platform + ('The role should be the same as AXL role: ' + self.role)
class AXL(Credential):
def __init__(self, path, platform, item):
super().__init__(path, platform)
self.role = 'r'
self.item = item
def final(self):
return self.username_file + self.item
reg1 = AXL(PATH, PLATFORM, ITEM)
print('AXL role:', reg1.role)
print(reg1.username_file)
print(reg1.final())
the result would be
AXL role: r
home_drive_Linux_The role should be the same as AXL role: rx
home_drive_Linux_The role should be the same as AXL role: rx_PC
Instead of rx, I need to see r
Here is a link to the sandbox
Answer by @chepner is definitely correct solution. I'll leave this here as a homage to over complicating a simple problem.
You could make username_file
a @property
on Credential
so it's evaluated on access not on instance creation.
class Credential:
def __init__(self, path, platform):
self.role = 'rx'
self.path = path
self.platform = platform
@property
def username_file(self):
return self.path + self.platform + self.role
If it's an instance of AXL
roll will be 'r' and 'rx' if instance of Credential
.
You can also cache the result of the first property access if you want to streamline:
class Credential:
def __init__(self, path, platform):
self.role = 'rx'
self.path = path
self.platform = platform
self._username_file = None
@property
def username_file(self):
if not self._usernme_file:
self._username_file = self.path + self.platform + self.role
return self._username_file
role
should be an parameter of Credential.__init__
, not hard-coded, although it could have a default value for the base class. Subclasses would pass the required role directly to super().__init__
. (If there is something that must have a role of 'rx'
, that should be a subclass of Credential
as well, not Credential
itself.)
class Credential:
def __init__(self, path, platform, role='rx'):
self.role = role
self.username_file = path + platform + ('The role should be the same as AXL role: ' + role)
# class SomethingNeedingRoleRX(Credential):
# def __init__(self, path, platform):
# super().__init__(path, platform, 'rx')
class AXL(Credential):
def __init__(self, path, platform, item):
super().__init__(path, platform, 'r')
self.item = item
def final(self):
return self.username_file + self.item
To play nicer with super
, you might consider using keyword-only arguments for __init__
:
class Credential:
def __init__(self, *, path, platform, role='rx', **kwargs):
super().__init__(**kwargs)
self.role = role
self.username_file = path + platform + role
class AXL(Credential):
def __init__(self, *, item, **kwargs):
super().__init__(role='r', **kwargs)
self.item = item
def final(self):
return self.username_file + self.item
reg1 = AXL(path=PATH, platform=PLATFORM, item=ITEM)
Ok, the problem here is basically the data type of username_file
; its a string created in the constructor and does not update when it's components do. There are a few ways to go around this, using properties is a pretty good and clean way to go about it:
PATH = 'home_drive_'
PLATFORM = 'Linux_'
ITEM = '_PC'
class Credential:
def __init__(self, path, platform):
self.path = path
self.platform = platform
self.role = 'rx'
self.username_file = self.path + self.platform + ('The role should be the same as AXL role: ' + self.role)
@property
def role(self):
return self._role
@role.setter
def role(self, new_role):
self._role = new_role
self.username_file = self.path + self.platform + ('The role should be the same as AXL role: ' + self.role)
class AXL(Credential):
def __init__(self, path, platform, item):
super().__init__(path, platform)
self.role = 'r'
self.item = item
def final(self):
return self.username_file + self.item
reg1 = AXL(PATH, PLATFORM, ITEM)
print('AXL role:', reg1.role)
print(reg1.username_file)
print(reg1.final())
EDIT:
Just a short explanation, you can turn to a property pretty much any variable, the reason for selecting role is resource management. Essentially, it takes fewer resources to update username_file
in the setter every time role changes (once in the program) than add strings (slow operation) every time you call the getter. Of course, considering the size of the program resource management should not be a deal breaker, but mentioning to explain the logic of the answer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.