Python Object Attributes And Methods
Solution 1:
This is easy to accomplish by overriding __dir__
and __getattribute__
:
class Empty(object):
def __dir__(self):
return []
def __getattribute__(self, name):
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, name))
e = Empty()
dir(e)
[]
e.__name__
AttributeError: 'Empty' object has no attribute '__name__'
(In python2, Empty
needs to be a new-style class, so the class Empty(object):
is required; in python3 old-style classes are extinct so class Empty:
is sufficient.)
Solution 2:
Havn't came across any such object, which doesn;t have any attribute.. see below
In [74]: class dummy():
....: pass
....:
In [75]: d1 = dummy()
In [76]: dir(d1)
Out[76]: ['__doc__', '__module__']
In [77]: len(dir(d1))
Out[77]: 2
even None has attributes...
In [78]: dir(None)
Out[78]:
['__class__',
'__delattr__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__']
Solution 3:
Yes! (or no...)
def AMeta(name, bases, dct):
class NoProp:
pass
del NoProp.__doc__
del NoProp.__module__
return NoProp
class A:
__metaclass__ = AMeta
print dir(A), 'len', len(dir(A))
print
print 'but... A.__name__ is', A.__name__
print 'Delete it!'
try:
del A.__name__
except Exception as e:
print 'Did not work: ', repr(e)
print
print 'and... A.__dict__ is', A.__dict__
print 'Delete it!'
try:
del A.__dict__
except Exception as e:
print 'Did not work: ', repr(e)
print
print 'and... A.__bases__ is', A.__bases__
print 'Delete it!'
try:
del A.__bases__
except Exception as e:
print 'Did not work: ', repr(e)
print
print 'What is the type of A?'
t = type(A)
print t, 'which is a', type(t)
print "All of these will raise an AttributeError:"
print "A.__class__, A.__module__, (and maybe some others which are usually there too...)"
Normally, all objects have some attributes whatever these are. But when using metaclasses, you can customize the way the class is created, and there you have it.
However, even if dir
is empty, you can still access A.__name__
, A.__dict__
, A.__bases__
.
This is what the tests I made gave me:
[] len 0
but... A.__name__ is NoProp
Delete it!
Did not work: TypeError('__name__ must be a string object',)
and... A.__dict__ is {}
Delete it!
Did not work: TypeError('__dict__ must be a dictionary object',)
and... A.__bases__ is ()
Delete it!
Did not work: TypeError('__bases__ must be a tuple object',)
What is the type of A?
<type 'classobj'> which is a <type 'type'>
All of these will raise an AttributeError:
A.__class__, A.__module__, (and maybe some others which are usually there too...)
Solution 4:
You can create an object without any "public" attributes and methods:
class Bare(object):
pass
But this object will have some internal/standard methods and attributes:
>>> x = Bare()
>>> dir(x)
['__class__',
'__delattr__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__']
Python has no concept of enforced private methods and attributes, everything is exposed. However, by convention, you should avoid external access to methods and attributes beginning with _
, these should be reserved for internal use (double underscores for Python internal methods). In practice you can check for an instance without any "public" attributes:
>>> filter(lambda a: a[0] != '_', dir(x))
[]
>>> len(filter(lambda a: a[0] != '_', dir(x)))
0
Even if you cheat by overriding __dir__
and __getattribute__
, the built-in attributes are still there and can be accessed using __getattribute__
from the parent class (thanks for martineau to point me this):
class FakeEmpty:
def __dir__(self):
return []
def __getattribute__(self, name):
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, name))
>>> e = FakeEmpty()
>>> object.__getattribute__(e, '__class__')
__main__.Empty
So the answer is: not really, but you can almost fake it.
Post a Comment for "Python Object Attributes And Methods"