I have a simple question. As everything is an object in python, can I access the inner attributes of a builtin object.
e.g. a='some String'
I want to access in the inner attributes of the string object a.
Here is what I’have tried:-
for x in dir(a):
if not callable(eval('a.' + x)):
print x
But, I get output as:
__doc__
But, I want to access other attributes from this object, which the object will be using for itself.
Is there a way through which I can access the abstract attributes of this object ?
Just to elaborate:
class Some(object):
def __init__(self, initialiser):
self.initialiser = initialiser
s = Some('Any object can be put here, I am using a string')
print s.initialiser ## This is how I'm accessing the attribute of the class Some. Similarly, can I ##access the attributes of the string object `a` defined above?
print s # gives me: <__main__.Some object at 0x02371AF0>
So, why does print a not give me such an output. Because, some method is called when I print a and which accesses the memory where the actual string sequence is stored and prints it. But, this does not happen when I print s
There is very little you cannot access in Python, as there are few, if any, private attributes to the Python build-in types.
You really want to go read up on the python datamodel, and study the inspect module (including it’s source code).
That’ll tell you all you need to know about what you can and cannot access in python.