Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accessing all the attributes written

I hope you’re doing well. I am writing this message, because I would like to have access to all the attributes written of an object. So in this case, below I would like to access the value of self.name, the value of self.age and the value of self.sex .
I tried with dir(self) but it doesn’t give me just these 3 above

class test(object):
    def __init__(self,uut):     

        self.name="name"
        self.age= "age"
        self.sex="male"

        self.cross_count = ccc.cross((att, getattr(self,att)) for att in dir(self))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

All of them are under __dict__

class test(object):
    def __init__(self):

        self.name = "name"
        self.age = "age"
        self.sex = "male"

        att = [(k, v) for k, v in (self.__dict__.items())]
        # __dict__ is just a simple python dictionary. You can use .values() if you want only values
        values = [v for v in (self.__dict__.values())]

        print(att)

test()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading