0

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

GodMan
  • 2,561
  • 2
  • 24
  • 40
  • 2
    What "inner variables" or "abstract variables" do you want to access? And what do you mean by those terms? – kindall Sep 04 '12 at 18:13
  • What do you mean by "inner variables"? – BrenBarn Sep 04 '12 at 18:13
  • 1
    What makes you think that there is such a thing? –  Sep 04 '12 at 18:16
  • 4
    Can you give a concrete example of an attribute you can't access on an object that you want to be able to? Also, use `getattr(a, x)`, not `eval()`. – Silas Ray Sep 04 '12 at 18:16
  • this looks like bad design ... maybe rethink what you are trying to accomplish – Joran Beasley Sep 04 '12 at 18:23
  • @delnan: I am thinking that just like I can access the attributes of any custom object, can I access the attributes of a builtin object – GodMan Sep 04 '12 at 18:24
  • Yes. Just use dot accessors like any other object. – Silas Ray Sep 04 '12 at 18:24
  • @sr2222: How do I know which attribute to use dot accessor with? I mean I should first know what attributes are present, then, only I can access them – GodMan Sep 04 '12 at 18:25
  • @GodMan I mean, what makes you think there are attributes on these objects? Sure, if there were some, you could access them. But what attributes would you expect on, say, an integer? BTW `dir()`'s result can be overriden by objects, but it is generally accurate. –  Sep 04 '12 at 18:26
  • 2
    That's why objects have documentation that describes their interfaces. If you really can't be sure what attributes there are on the object, introspection (`dir()` for example) can tell you. – Silas Ray Sep 04 '12 at 18:26
  • 1
    @GodMan: many builtin objects have no attributes. This is because they are implemented in C as C structs, and their data is not exposed as Python attributes. – Ned Batchelder Sep 04 '12 at 18:29
  • You can access anything that is there. The question is, what do you think is there to access? – BrenBarn Sep 04 '12 at 18:30
  • @NedBatchelder Here's some proof to back up your assertion: http://hg.python.org/cpython/file/2370e331241b/Objects/stringobject.c – Wilduck Sep 04 '12 at 18:34

3 Answers3

2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    For the OP's specific question, you could also include a link to the source for python's built in objects (http://hg.python.org/cpython/file/c6880edaf6f3/Objects), or if OP is particularly concerned about the built in string object, http://hg.python.org/cpython/file/c6880edaf6f3/Objects . – Wilduck Sep 04 '12 at 18:33
  • 2
    @Wilduck: the data model explains most; to start mixing in the C implementations (which requires an understanding of the C API) is a little too much to ask at this stage, I fear. – Martijn Pieters Sep 04 '12 at 18:36
0

Unless overridden, dir() will provide you with a list of all of the non-mangled attributes on an object (and if overridden it's usually for a good reason). The reason you get __doc__ as your output is it's the only non-callable attribute on the string type, the rest are methods. You can access it directly via normal attribute look-up:

>>> s = 'string'
>>> print s.__doc__
str(object) -> string

Return a nice string representation of the object.
If the argument is a string, the return value is the same object.

What other attributes were you expecting the str type to have?

As for why printing a string gives you the string while printing a class gives you a more abstract representation: print calls the __str__ method on an object, which allows for a human friendly display to be provided. For strings, this is quite obviously the string itself. For classes, a more generic default is used.

Matthew Trevor
  • 14,354
  • 6
  • 37
  • 50
  • As you said, __str__ allows for a human friendly display to be provided. I have a question on that: Human friendly display of what?? the string object must be holding a variable which the __str__ accesses to provide a human friendly display. is it ? – GodMan Sep 05 '12 at 09:10
  • `str` is implemented in C: however it stores the string internally, that isn't directly exposed to the interpreter. You might find this article on [Python string object implementation](http://www.laurentluce.com/posts/python-string-objects-implementation/) interesting. The bigger question here is: what are you trying to achieve from this? – Matthew Trevor Sep 05 '12 at 11:18
  • I am trying to increase my knowledge on Python internals by asking this question. – GodMan Sep 05 '12 at 14:50
  • "the string object must be holding a variable which the str accesses to provide a human friendly display". Yes; the string object *is* that variable. – kindall Sep 06 '12 at 16:04
-1

When you do print obj, python calls the __str__ method on the object. For a string, the result is obvious, but for your custom classes, the default __str__ method calls __repr__, which returns some useful information. I'm pretty sure this doesn't answer your actual question, just the question you actually asked.

gcbirzan
  • 1,494
  • 11
  • 17