Sample code:
class hello:
cls_var_name='Hello_Class'
def setdata(self, value):
self.data=value
fun1_var_name='Setdata_Function'
def showdata(self):
fun2_var_name='Showdata_Function'
print(self.data)
So, With the help of the dir() function, we can list all the attribute of a class. but how to list out variables/attributes inside the function of a class.
Demo:
>>> dir(hello)
['__doc__', '__module__', 'cls_var_name', 'setdata', 'showdata']
>>>
>>> dir(hello.setdata)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__func__', '__get__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'im_class', 'im_func', 'im_self']
>>>
>>> dir(hello.showdata)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__func__', '__get__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'im_class', 'im_func', 'im_self']
In the above output you can seen the list of attribute of a class in dir(hello) result:
'cls_var_name', 'setdata', 'showdata'
But for dir(hello.setdata) and dir(hello.showdata) result it list out all the related attribute but not a fun1_var_name and fun2_var_name variable, why?