Instantiating classes which are derived from built-in types.

Discussion in 'Python' started by Alex Martelli, Nov 29, 2005.

  1. Achim Dahlhoff <> wrote:

    > Hi.
    >
    > I'm trying to find out the diffrence between normal classes and classes
    > derived from built-in types.
    > (Which is causing me trouble trying to instantiate a class using C API
    > calls)
    >
    > >>> class A:

    > ... pass
    > ...
    > >>> class B(dict):

    > ... pass
    > ...
    > >>> type(A)

    > <type 'classobj'>
    > >>> type(B)

    > <type 'type'>
    > >>>


    A is oldstyle -- a wart existing for backwards compatibility. Newstyle
    classes (highly recommended for all new code) may inherit from object
    (or any other newstyle class, or builtin type supporting inheritance) or
    may be set on a per-module basis by setting __metaclass__=type at module
    level at the start of the module.

    > Anyone know how an object could be instantiated using a handle to B?


    Just use the recommended abstract interface, e.g.
    PyObject_CallFunction(B, NULL) if you want to pass no arguments - this
    works for A, for B, and for any other callable including e.g. a factory
    function (its very generality makes it very desirable...).


    Alex
     
    Alex Martelli, Nov 29, 2005
    #1
    1. Advertising

  2. Alex Martelli

    Paul Rubin Guest

    (Alex Martelli) writes:
    > A is oldstyle -- a wart existing for backwards compatibility.


    I think it's time for "from __future__ import newclasses" since
    I hate having to type "class A(object):" instead of "class A:"
    all over the place.
     
    Paul Rubin, Nov 29, 2005
    #2
    1. Advertising

  3. Paul Rubin <http://> wrote:

    > (Alex Martelli) writes:
    > > A is oldstyle -- a wart existing for backwards compatibility.

    >
    > I think it's time for "from __future__ import newclasses" since
    > I hate having to type "class A(object):" instead of "class A:"
    > all over the place.


    Use __metaclass__=type exactly in the same location where you'd want to
    import from __future__ and you will get exactly the same functionality.


    Alex
     
    Alex Martelli, Nov 29, 2005
    #3
  4. Hi.

    I'm trying to find out the diffrence between normal classes and classes
    derived from built-in types.
    (Which is causing me trouble trying to instantiate a class using C API
    calls)

    >>> class A:

    .... pass
    ....
    >>> class B(dict):

    .... pass
    ....
    >>> type(A)

    <type 'classobj'>
    >>> type(B)

    <type 'type'>
    >>>



    When I have a handle to A as a PyObject, I can create an instance using
    PyInstance_New(). When I have a handle to B, this does not work as the
    function wants a class PyObject.
    I found that the API function PyType_GenericNew() can create something from
    the B type, but it does not call any constructors.

    (using the 'dict' here is an example. In my case, I'm using a self-defined
    type I'm using as an API into the C++ part of the software.)

    Anyone know how an object could be instantiated using a handle to B?


    thanks,


    Achim Dahlhoff.
     
    Achim Dahlhoff, Nov 29, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alona
    Replies:
    2
    Views:
    670
  2. Randy
    Replies:
    3
    Views:
    349
    Old Wolf
    May 15, 2007
  3. Replies:
    1
    Views:
    425
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    412
    Victor Bazarov
    May 23, 2007
  5. Trevoke
    Replies:
    10
    Views:
    213
    Brian Candler
    Jul 22, 2009
Loading...

Share This Page