Passing argument from method using super

I'm trying to set an argument role='r' under a method called getphone. It's working ok under init using super but I can't figure out how to do it under another method

The role is to set the permission level for the api that is running

this code is working

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())

under the class AXL, I want to move the role='r' under the method getphone

I've tried this and it's working but i don't understand why i need to put the path and platform.

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())

a sandbox can be found here

Upvotes: 0

Views: 78

Answers (2)

geckos
geckos

Reputation: 6299

super() will let you access the parent class scope. You can access any method from it in the form super().method(args) in python3, with python 2 the format is super(YourClass,self).method(args).

Calling __init__ on the parent for each method doesn't seems to be right from the OOP perspective. You are reinitializing the parent object during its life time. If you want to set role from the parent you can simply use self.role = .... Any method on parent accessing self.role will see the modification.

Upvotes: 0

chepner
chepner

Reputation: 531858

There's no one role associated with an instance; rather, the role is associated with a method called by the instance. Try this.

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())

Upvotes: 1

Related Questions