Is there a Python way to access all non-private and non-built-in class attributes?

I would like to call a method to give me a request for all "non-private" ones (I use the term "private" here a little weakly, since it really does not exist in Python) and non-built-in attributes (i.e. those that don’t begin with single or double underscores) on the class. Something like vars (MyClass), which will return only the "public" attributes of this class.

I know that

from M import * 

does not import objects whose name begins with an underscore. ( http://www.python.org/dev/peps/pep-0008/#id25 ) How does import implement this? Through a built-in function, or simply by checking for underscores? What is the pythonic way to do this?

Example:

 class MyClass(object): def __init__(self): do_stuff() def _private(self): print 'private' def __gets_name_mangled(self: print 'becomes _MyClass__gets_name_mangled()' def public(self): print 'public' 

If i do

 vars(MyClass).keys() 

I get

 ['_MyClass__gets_name_mangled', '__module__', '_private', '__doc__', '__dict__', '__weakref__', 'public', '__init__'] 

How can i only get

 ['public'] 

Or do I just need to check to emphasize myself? It seems like this will be a pythonic way of doing this.

For more information about underscores and double underscores, see What is the meaning of single and double underscores in front of an object name?

+6
source share
3 answers

Actually, for such a function it would be irregular, because "officially" there are no private or protected fields / properties in Python.

While it makes sense to drop module attributes with leading underscores (which are usually implementation details) during import * from some module *, this is not useful in the context of any other object.

So, if you need to list only the "public" methods / attributes of the object, just go to the dir result and discard the names with leading underscores.


* "during import * from some module"

This is usually not the best practice. Consider the following example:

module A has a1 and a2 defined

module B has b1 and b2 defined

This code in module C works as expected:

 from A import a1, a2 from B import * 

Imagine that we add function a1 to module B Now suddenly the module C broken, although we did not touch it.

+4
source

With a dict understanding that filters vars ()

 { k:v for k,v in vars(myObject).items() if not k.startswith('_') } 

Moves to a function that returns a list of attributes that are not soft quotes or callers. You can return the values ​​if you wish by going to the dict definition as described above.

 def list_public_attributes(input_var): return [k for k, v in vars(input_var).items() if not (k.startswith('_') or callable(v))] 
+2
source

I am using this function:

 def print_all_public_fields(obj): print(obj) for a in dir(obj): if not a.startswith('_') and not a.isupper(): print('\t%s = %s' % (a, getattr(obj, a))) 

I know that this is not exactly what you want, but perhaps it will give you some idea.

+1
source

Source: https://habr.com/ru/post/947159/


All Articles