Rajesh
Rajesh

Reputation: 720

How to list the variables or attribute inside a function of a class?

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?

Upvotes: 2

Views: 725

Answers (1)

Alexander
Alexander

Reputation: 17355

The reason is because the dir function displays a list of properties and attributes that are bound to an object. fun_var_name is not an attribute of the setdata method, it is a variable that lives within the scope of the method body and discarded as soon as it exits.

So when you call dir(hello.showdata) unless the call is made inside the showdata method, func2_var_name doesn't exist, however even if it was, the variable still would not appear using the dir function because it is not bound to the callable method object. (A "callable method object" is simply another term for a method, but it emphasizes the fact that methods, like pretty much everything in python, are also objects.)

If what you desire is the means of accessing a list of variable names you can use the vars() function, but it will only show variables that have already been defined and are within the current scope. like this:

class hello:
    cls_var_name='Hello_Class'

    def setdata(self, value):
        self.data=value
        fun1_var_name='Setdata_Function'
        print(vars())

    def showdata(self):
        fun2_var_name='Showdata_Function'
        print(self.data)
        print(vars())

Getting the variable names from outside of the method body isn't possible because those variables don't exist.

A case could be made for using the inspect module and getting the actual source code, but I don't think that is what you had in mind.

Upvotes: 6

Related Questions