E88
E88

Reputation: 107

Python pass attribute from one method to another

I'm trying to pass attribute from one method to another, not sure how I to pass for other method to another. Below code valuename and check_name are two attributes I want to pass and access by printvalue method. any thought?

class Load_dict(object):
    def __init__(self, data):
        for name, value in data.items():
            setattr(self, name, self._wrap(value) if (value is not None) else '')
    
    def _wrap(self, value):
        if isinstance(value, (tuple, list, set, frozenset)):
            return type(value)([self._wrap(v) for v in value])
        else:
            return Load_dict(value) if isinstance(value, dict) else value

    def delta(self):
        d= list(self.lived_state.split(','))
        return(d) 

    def gg(self):
        if 'IL' in self.delta():
            print(self.delta())
        else:
            print('not')

class load_process(Load_dict):
    def __init__(self, value):
        super().__init__(value)
    
    def display(self):
        self.valuename = 'hello'
        self.check_name = self.fname if self.Age > 30 else self.lname
        print('Check :  and lived State : {} and values - {}'.format( self.lived_state, self.Description.values))
    
    def printvalue(self):
        print(self.valuename)
        print(self.check_name)

d = {'fname':'John', 'lname': 'Burns', 'Age': 34, 'Gender': None, 'State' : 'CA', 'lived_state' : 'CA,TX,NY,FL'}
e = {'Code': 'Dev', 'Description' : {'name' : 'Development', 'environment' : 'dev11', 'node' : '', 'values' : ['222', '333', '444']}}
b = load_process({**d, **e})

print(b.delta())
b.printvalue()

error

AttributeError: 'load_process' object has no attribute 'valuename'

Upvotes: 0

Views: 111

Answers (1)

rhurwitz
rhurwitz

Reputation: 2737

The problem is that your are calling printvalue on b before valuename and checkname have been initialized by b. The only code in your load_process class that initializes valuename or check_name is in display. If you want to make sure that valuename and check_name have been initialized before calling printvalue then you should initalize those attributes in your __init__ method in the load_process class.

Upvotes: 1

Related Questions