Hello,

Python objects are supposed to be mainly a structure composed of a (pointer to 
a) type and a (pointer to a) value; and to be more or less implemented that way 
in the C version.

When an object is of a standard type like a number, the value field would then 
point to a C value, or rather in most cases to a custom piece of data built on 
top of C values. Anyway, this is transparent on the python language side. For 
an int, for instance, the actual value, in the common sense of the term, is 
unreachable: the python object has or is equivalent to a value, but does not 
provide a .value attribute: indeed, it would be a C thingie. In fact, since the 
object structure's value field points to this value, there is no place for a 
set of attributes (a dict) like custom objects have:
        i = 1 ; i.name = "one"  # -->
        AttributeError: 'int' object has no attribute 'name'
One can still address predefined fields of standard objects:
        i = 1 ; print i.__add__ # -->
        <method-wrapper '__add__' of int object at 0x8435d60>
But in fact the method __add__ is found on the class int, not on i; and as the 
text says it's not even really a method.

Instead this works, indeed:
        class C(object): pass
        c = C() ; c.name = 'c'
Precisely, custom objects have a dict to store arbitrary attributes. But they 
have no value in the common sense. Or rather their value, in the sense of what 
is pointed by the value field of the structure representing this object, would 
precisely be a dict representing a set of attributes.
If this holds true, then an object either has a transparent and inaccessible 
value in the common sense of the term (eg 1), or a dict of attributes (eg 
{name:'x',count:3}), which are accessible on the python side.

Comments, critics, corrections welcome.

Now, one can combine both, for instance by subtyping a standard type:

class Integer(int): pass
i = Integer(1)
i.name = "one"
print i,i.name  # --> "1 one"

An instance of Integer has both a value 1 and a dict. Actually, one can do more 
sophisticated things (below __new__ is needed because of some limitation in 
float):

class Constant(float):
    def __new__(cls, name, value):
        obj = float.__new__(cls, value)
        obj.name = name
        return obj
    def __str__(self):
        return "%s:%s" %(self.name, float.__str__(self))
PI = Constant("pi", 3.14)
print PI      # --> "pi:3.14"

In either case, the actual numerical value is transparently stored and 
inaccessible: reason why I need to use float.__str__ to print it out. But there 
also is a dict available to store attributes.

So, a question arises: does the structure representing a Constant object like 
PI have one more field? Namely for type, value *and* dict?


Denis
________________________________

vit esse estrany ☣

spir.wikidot.com
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to