getting a dictionary of class variables and values

后端 未结 3 1842
余生分开走
余生分开走 2021-02-04 02:39

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条回答
  •  独厮守ぢ
    2021-02-04 03:15

    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}
    

提交回复
热议问题