getting a dictionary of class variables and values

27,232

Solution 1

You need to filter out functions and built-in class attributes.

>>> class A:
...     a = 3
...     b = 5
...     c = 6
... 
>>> {key:value for key, value in A.__dict__.items() if not key.startswith('__') and not callable(key)}
{'a': 3, 'c': 6, 'b': 5}

Solution 2

Something like this?

  class A(object):
      def __init__(self):
          self.a = 3
          self.b = 5
          self.c = 6

  def return_class_variables(A):
      return(A.__dict__)


  if __name__ == "__main__":
      a = A()
      print(return_class_variables(a))

which gives

{'a': 3, 'c': 6, 'b': 5}

Solution 3

Use a dict comprehension on A.__dict__ and filter out keys that start and end with __:

>>> class A:
        a = 3
        b = 5
        c = 6
...     
>>> {k:v for k, v in A.__dict__.items() if not (k.startswith('__')
                                                             and k.endswith('__'))}
{'a': 3, 'c': 6, 'b': 5}
Share:
27,232

Related videos on Youtube

Plaix
Author by

Plaix

About me `

Updated on July 09, 2022

Comments

  • Plaix
    Plaix almost 2 years

    I am working on a method to return all the class variables as keys and values as values of a dictionary , for instance i have:

    first.py

    class A:
        a = 3
        b = 5
        c = 6
    

    Then in the second.py i should be able to call maybe a method or something that will return a dictionary like this

    import first
    
    dict = first.return_class_variables()
    dict
    

    then dict will be something like this:

    {'a' : 3, 'b' : 5, 'c' : 6}
    

    This is just a scenario to explain the idea, of course i don't expect it to be that easy, but i will love if there are ideas on how to handle this problem just like dict can be used to set a class variables values by passing to it a dictionary with the variable, value combination as key, value.

    • Bi Rico
      Bi Rico over 10 years
      It's not clear what you're asking, what do you mean by 'class variables'?
    • Plaix
      Plaix over 10 years
      Class attributes, the answers below was what i was looking for.:)
    • Bi Rico
      Bi Rico over 10 years
      k, just keep in mind that will also include methods of the class and will not give you any attributes defined in a parent class.
    • Plaix
      Plaix over 10 years
      Yes, the class i am working on contains only attributes no methods, thank you i will keep that in mind
  • SW_user2953243
    SW_user2953243 over 8 years
    I would recommend renaming the class...for added clarity with respect to the return_class_variables() method.
  • realgeek
    realgeek over 7 years
    Minor correction: I think that should be ...and not callable(value)}.
  • Eric
    Eric over 5 years
    You're setting and returning instance variables, not the class variable. Class variables are not defined in __init__() but just after the class statement. Class variables are common to all instances, whereas instance variables belong to the instance only (the Class object can't access instance variables, although the opposite is possible).