JoeDainton123
JoeDainton123

Reputation: 129

How to succinctly list all attributes of classes and objects?

I have started to create classes and objects in python, progress is slow but I am getting there. I wanted to ask the community whether it was possible to output all the attributes of a class/object.

I have created the following class:-

import inspect

class Helloclass:

    def __init__(self):
        self.A = ""
        self.B = ""
        self.C = ""
        self.D = ""

london = Helloclass()
london.A = "Apples"
london.D = 120

print(inspect.getmembers(london))
  ('A', 'Apples'), ('B', ''), ('C', ''), ('D', 120), ('__class__', <class '__main__.Helloclass'>),
  ('__delattr__', <method-wrapper '__delattr__' of Helloclass object at 0x000001E6B704CD00>),
  ('__dict__', {'A': 'Apples', 'B': '', 'C': '', 'D': 120}), ('__dir__', <built-in method __dir__ of
  Helloclass object at 0x000001E6B704CD00>), ('__doc__', None), ('__eq__', <method-wrapper '__eq__' of
  Helloclass object at 0x000001E6B704CD00>), ('__format__', <built-in method __format__ of Helloclass
  object at 0x000001E6B704CD00>), ('__ge__', <method-wrapper '__ge__' of Helloclass object at
  0x000001E6B704CD00>), ('__getattribute__', <method-wrapper '__getattribute__' of Helloclass object at
  0x000001E6B704CD00>), ('__gt__', <method-wrapper '__gt__' of Helloclass object at
  0x000001E6B704CD00>), ('__hash__', <method-wrapper '__hash__' of Helloclass object at
  0x000001E6B704CD00>), ('__init__', <bound method Helloclass.__init__ of <__main__.Helloclass object
  at 0x000001E6B704CD00>>), ('__init_subclass__', <built-in method __init_subclass__ of type object at
  0x000001E6B57CF350>), ('__le__', <method-wrapper '__le__' of Helloclass object at
  0x000001E6B704CD00>), ('__lt__', <method-wrapper '__lt__' of Helloclass object at
  0x000001E6B704CD00>), ('__module__', '__main__'), ('__ne__', <method-wrapper '__ne__' of Helloclass
  object at 0x000001E6B704CD00>), ('__new__', <built-in method __new__ of type object at
  0x00007FFF9F7DCB50>), ('__reduce__', <built-in method __reduce__ of Helloclass object at
  0x000001E6B704CD00>), ('__reduce_ex__', <built-in method __reduce_ex__ of Helloclass object at
  0x000001E6B704CD00>), ('__repr__', <method-wrapper '__repr__' of Helloclass object at
  0x000001E6B704CD00>), ('__setattr__', <method-wrapper '__setattr__' of Helloclass object at
  0x000001E6B704CD00>), ('__sizeof__', <built-in method __sizeof__ of Helloclass object at
  0x000001E6B704CD00>), ('__str__', <method-wrapper '__str__' of Helloclass object at
  0x000001E6B704CD00>), ('__subclasshook__', <built-in method __subclasshook__ of type object at
  0x000001E6B57CF350>), ('__weakref__', None)

Although this gives me what i want, there is so much extra stuff which I do not need or want to see.

I just want the output to be something like:-

london Object of Class Helloclass
london.A = "Apples"
london.B = ""
london.C = ""
london.D = 120

Is there any way of getting cleaner output?

Upvotes: 1

Views: 1605

Answers (2)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

You could simply do this to get both.

import inspect
 
class Helloclass:
     
    def __init__(self):
        self.A = ""
        self.B = ""
        self.C = ""
        self.D = ""
        
    def __str__(self):
        return  str(self.__class__) + '\n' + '\n'.join((str(item) + ' = ' + str(self.__dict__[item]) for item in sorted(self.__dict__)))
 
london = Helloclass()
london.A = "Apples"
london.D = 120

print(london)

Should produce something close

<class '__main__.Helloclass'>                                                                                                        
A = Apples                                                                                                                           
B =                                                                                                                                  
C =                                                                                                                                  
D = 120   

You could also use self.class.name and change up the string however you want.

 def __str__(self):
        return  'Object of Class '+str(self.__class__.__name__ ) + '\n' + '\n'.join((str(item) + ' = ' + str(self.__dict__[item]) for item in sorted(self.__dict__)))

It should be closer.

Object of Class Helloclass                                                                                                           
A = Apples                                                                                                                           
B =                                                                                                                                  
C =                                                                                                                                  
D = 120  


                                                                                                                       
      

Upvotes: 1

Cory Kramer
Cory Kramer

Reputation: 117856

You could use vars to get this kind of output

>>> vars(london)
{'A': 'Apples', 'B': '', 'C': '', 'D': 120}

Upvotes: 0

Related Questions