-1

Below is the simple code to fetch instance variable list and its value.

Below code provides output of list instance variables and its value. I required help to find static variables list and its value.

 class Test:
    x = 10
    def __init__(self):
        self.a =10

 t = Test()       
 print("Instance Variables:",t.__dict__)

Actual Results:

 Instance Variables: {'a': 10}

Expected results:

 Instance Variables: {'a': 10}
 Static Variable: {'x':10}
sagar_dev
  • 83
  • 2
  • 10
  • 3
    The term 'static' has not got the same meaning in Python as it does in Java or C++. Python's class namespace is just another dictionary namespace just like instances can have a namespace. Functions and integers are just two types of Python objects, you can have any number of types of objects referenced in a class namespace. – Martijn Pieters Jan 04 '19 at 12:21

1 Answers1

1

Classes have a lot of attributes automatically, but they tend to start with __. So to get the rest, you can use something like:

{k:v for k,v in Test.__dict__.items() if not k.startswith('__') }

Result:

{'x': 10}
khelwood
  • 55,782
  • 14
  • 81
  • 108