I would like to call a method to give me a dict of all of the "non-private" (I use the term "private" somewhat loosely here since it does not really exist in Python) and non-builtin attributes (i.e. those that do not begin with a single or double underscore) on a class. Something like vars(MyClass) that would return only the "public" attributes on that class.
I'm aware that
from M import *
does not import objects whose name starts with an underscore. (http://www.python.org/dev/peps/pep-0008/#id25) How does import implement that? Via a builtin function or just 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 get only
['public']
Or do I just need to check for underscores myself? It just seems like there would be a pythonic way to do this.
For more on underscores and double underscores, see: What is the meaning of a single- and a double-underscore before an object name?