FAQ
Hello,

I am attempting to fully-simulate an 'int' object with a custom object type.
It is part of a library I am creating for python futures and promises. Is
there anyway such that type(my_object) can return <type 'int'>? Or for that
matter, any other primitive? I do not care how dirty the solution might
possibly be; Could there be a way through the C API? Though I predict it
will cause a mess with the interpreter.

Thanks,

Ken Elkabany
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090206/86425f34/attachment.htm>

Search Discussions

  • Chris Rebert at Feb 6, 2009 at 11:42 am

    On Fri, Feb 6, 2009 at 3:03 AM, Ken Elkabany wrote:
    Hello,

    I am attempting to fully-simulate an 'int' object with a custom object type.
    It is part of a library I am creating for python futures and promises. Is
    there anyway such that type(my_object) can return <type 'int'>? Or for that
    matter, any other primitive? I do not care how dirty the solution might
    possibly be; Could there be a way through the C API? Though I predict it
    will cause a mess with the interpreter.
    Any particular reason why subclassing 'int' wouldn't be good enough?
    (You did know you can do that, right?)
    I know you can subclass int at the Python level and I would think you
    should be able to do it at the C level also (if not, the design just
    gets only slightly more involved).

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
  • Ken Elkabany at Feb 6, 2009 at 11:49 am
    I would simply subclass 'int', but this object needs to be general enough to
    pretend to be an 'int', 'NoneType', 'str', etc... A long shot: Can I change
    the base class on an instance by instance basis depending on the need? Well,
    now I can imagine having a class factory that will spawn for me the class
    that inherits the correct base type. Any other solutions?

    Thanks Chris.
    On Fri, Feb 6, 2009 at 3:42 AM, Chris Rebert wrote:
    On Fri, Feb 6, 2009 at 3:03 AM, Ken Elkabany wrote:
    Hello,

    I am attempting to fully-simulate an 'int' object with a custom object type.
    It is part of a library I am creating for python futures and promises. Is
    there anyway such that type(my_object) can return <type 'int'>? Or for that
    matter, any other primitive? I do not care how dirty the solution might
    possibly be; Could there be a way through the C API? Though I predict it
    will cause a mess with the interpreter.
    Any particular reason why subclassing 'int' wouldn't be good enough?
    (You did know you can do that, right?)
    I know you can subclass int at the Python level and I would think you
    should be able to do it at the C level also (if not, the design just
    gets only slightly more involved).

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20090206/c5cd0691/attachment.htm>
  • Christian Heimes at Feb 6, 2009 at 12:05 pm

    Ken Elkabany schrieb:
    I would simply subclass 'int', but this object needs to be general enough to
    pretend to be an 'int', 'NoneType', 'str', etc... A long shot: Can I change
    the base class on an instance by instance basis depending on the need? Well,
    now I can imagine having a class factory that will spawn for me the class
    that inherits the correct base type. Any other solutions?
    No, it's not possible. The type is an inherent part of an object. Python
    looks up methods and other attributes by looking at the type. The method
    call obj.method() is implemented as type(obj).method(obj).

    Christian
  • Terry Reedy at Feb 6, 2009 at 6:30 pm

    Ken Elkabany wrote:
    Hello,

    I am attempting to fully-simulate an 'int' object with a custom object
    type. It is part of a library I am creating for python futures and
    promises. Is there anyway such that type(my_object) can return <type
    'int'>? Or for that matter, any other primitive? I do not care how dirty
    the solution might possibly be; Could there be a way through the C API?
    Though I predict it will cause a mess with the interpreter.
    type(o) == o.__class__

    o.__class__ can only be rebound if and only if it is a user-class (heap
    type) and then only to another (compatible) user-class (heap type).
  • Pekka Klärck at Feb 8, 2009 at 1:20 pm
    2009/2/6 Ken Elkabany <Ken at elkabany.com>:
    I am attempting to fully-simulate an 'int' object with a custom object type.
    It is part of a library I am creating for python futures and promises. Is
    there anyway such that type(my_object) can return <type 'int'>?
    If it's enough to change how the type looks like you can simply change its repr:
    class X(type):
    ... def __repr__(self):
    ... return 'xxx'
    ...
    class Y(object):
    ... __metaclass__ = X
    ...
    type(Y())
    xxx


    Cheers,
    .peke

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 6, '09 at 11:03a
activeFeb 8, '09 at 1:20p
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase