Accessing other classes attributes in Python

78 Views Asked by At

I want to design specific class hierarchy consisting of three classes: Certificate, Organization, User. Both Certificate and Organization seem to me to be kinda "equal" (they are each others attributes and are dependent on each others):

class Certificate:
    def __init__(self,
                 certificate_id: int,
                 private_key_id: int,
                 organization: Organization = None, # Organization as an attribute!!!):
        self.organization = Organization(organization.name, organization.unit, organization.parent_id)
        self.organization.set_certificate_id(certificate_id)
        if organization.parent_id is not None:
            self.certificate_id: int = organization.parent_id
        else:
            self.certificate_id: int = certificate_id
        self.private_key_id: int = private_key_id
class Organization:
    def __init__(self,
                 name: str,
                 unit: str,
                 parent_id: int):
        self.name: str = name
        self.unit: str = unit
        self.parent_id: int = parent_id
        self.certificate_id = None

    def set_certificate_id(self, x):
        self.certificate_id = x

    def __get_certificate_id(self):
        return self.certificate_id

And then there is User class. User belongs to 1:N Organizations and has an access to their certificate ID's so it has Organization object as one of its attributes but I cannot access to its certificate - even though I defined a getter for Organization class:

class User:
    access = ("admin", "user", "manager"),

    def __init__(self,
                 name: str,
                 role: str,
                 **kwargs: Organization
                 # org: Organization 
                 # **kwargs
                 ):
        self.name = name,
        self.role = role,
        self.orgs = kwargs
        # for key, value in kwargs.iteritems():
        #     setattr(self, key, value)
        # self.orgs = {}

    def use_id(self):
        return self.orgs._Certificate__get_certificate_id()

Last line is kinda random, I tried accessing certificate in every way which came to my mind, ofc starting with self.orgs.certificate_id, what I get is (mostly) warning "Unresolved attribute reference '...' for class 'dict'" How am I supposed to access the ID of Certificate of given Organization which my User belongs to?

BTW, kwargs in init: I want to be able to set an undefined number of Organization objects as parameter, possibly as dictionary [Org: Certificate_id]

0

There are 0 best solutions below