[pypy-dev] Builtin types

Thomas Heller theller at python.net
Fri Jan 24 08:46:57 CET 2003


Christian Tismer <tismer at tismer.com> writes:

> classmethod works for every kind of class,
> may it be a newstyle class, triggered by
> - deriving from object
> - using __slots__
> - deriving from a builtin type or a descendant
> - did I forget something?
> or a "classic" class, it always works.
> For reference, see
> http://www.python.org/2.2/descrintro.html
[...]
> No, sorry. object is just a special case for deriving a
> new-style class, see above.
> They can also be created by deriving from builtin types,
> or by using __slots__.

That's incorrect, IIUC.

__slots__ only has a special meaning for *new stype classes* only,
it doesn't trigger anything in classes classes.

New style classes always have object as *one* of it's base
classes, and most builtin types are new style classes also.

> 
> Furthermore, I'm going to propose an extension to the new
> class system (at least for the MiniPy prototype) that goes
> a bit further:
> - __slots__ should get the ability to denote the type of
>    a slot to be generated, especially ctypes types

I'm not really understanding what you're proposing here.

You could look at ctypes as implementing 'typed slots' with
C-compatible layout.

class A(object):
    __slots__ ["x", "y", "z"]

class B(ctypes.Structure):
    _fields_ = [("x", "c"),
                ("y", "i"),
                ("z", "q")]
    __slots__ = []

Instances of both A and B can only have 'x', 'y', and 'z' instance
variables (or should I say slots), both don't have a __dict__.

> - it should be possible to derive a class from nothing,
>    not even from object or classic, but I'd like to describe
>    a plain C structure by classes.

This is maybe also something that ctypes already does.
The B *class* above knows all about this C structure

struct B {
    char x;
    int y;
    long long z;
};

>>> ctypes.sizeof(B)
16
>>>

> The latter will allow to define the object class and all
> builtin types with the same machinery.
> 

Thomas



More information about the Pypy-dev mailing list