Type question

Discussion in 'Python' started by nitrogenycs@web.de, May 17, 2005.

  1. Guest

    Hello,

    is this call assumed to be True in any case?

    result = type(SomeClass) is SomeClass

    I've written a proxy class which shadows a real object. If you call
    type(proxyobj) it returns the type of the proxyobject and not the type
    of the shadowed object. Example:

    p = proxy(shadowobj())
    result1 = type(p) is shadowobj # will return False
    result2 = isinstance(p, shadowobj) # will return True

    So the first call compares the id()s of both types, while the second
    calll seems to work different.
    I've tried to use a proxy metaclass that creates new objects with the
    name 'shadowobj'. So

    print type(p)
    print type(shadowobj())

    will look exactly the same. however their id()s compare different and
    that's why the 1st test doesn't work as I'd expect it to work.
    Can somebody tell me why the id()s compare different for the same type
    names? Here's the metaclass:

    class ProxyMeta(type):
    def __new__(self, classname, bases, classdict):
    return type.__new__(self, 'shadowobj', bases, classdict)

    class Proxy(object):
    __metaclass__ = ProxyMeta

    Why is (type(Proxy) is shadowobj == False)? Shouldn't type.__new__
    reuse the existing shadowobj type and increase its refcount instead of
    creating a new instance of it? Then the id()s would compare the same.
    So, finally, is checking for types with 'is' as shown above just wrong
    and one should use isinstance or is my proxy class showing bad
    behaviour or is this a bug in type.__new__?

    -Matthias
    , May 17, 2005
    #1
    1. Advertising

  2. Serge Orlov Guest

    wrote:
    > Hello,
    >
    > is this call assumed to be True in any case?
    >
    > result = type(SomeClass) is SomeClass
    >
    > I've written a proxy class which shadows a real object. If you call
    > type(proxyobj) it returns the type of the proxyobject and not the

    type
    > of the shadowed object. Example:
    >
    > p = proxy(shadowobj())
    > result1 = type(p) is shadowobj # will return False
    > result2 = isinstance(p, shadowobj) # will return True
    >
    > So the first call compares the id()s of both types, while the second
    > calll seems to work different.
    > I've tried to use a proxy metaclass that creates new objects with the
    > name 'shadowobj'. So
    >
    > print type(p)
    > print type(shadowobj())
    >
    > will look exactly the same.


    isinstance is not a law. It's just a convention. You don't
    actually need to implement it. Consider PEP 246, I believe,
    it is the right way to do what you want to do.
    isinstance and metaclasses are not.

    Instead of writing
    if not isinstance(obj, class):
    report error

    ask your users to write
    obj = adapt(obj, Interface)

    instead of dispatch code like
    if isinstance(obj, thisclass):
    do this
    elif isinstance(obj, thatclass):
    do that
    else:
    report error

    ask the users to write:
    if kind(obj) is ThisKind:
    do this
    elif kind(obj) is ThatKind:
    do that
    else:
    report error

    where kind can be defined as
    def kind(obj):
    return getattr(obj, 'kind', None)

    Serge.
    Serge Orlov, May 17, 2005
    #2
    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. Toby
    Replies:
    3
    Views:
    3,060
    Mike Treseler
    Sep 7, 2005
  2. heyo
    Replies:
    3
    Views:
    880
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    778
    Dan Pop
    Apr 2, 2004
  4. Yevgen Muntyan

    #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type)))

    Yevgen Muntyan, Feb 9, 2007, in forum: C Programming
    Replies:
    10
    Views:
    878
    Yevgen Muntyan
    Feb 13, 2007
  5. kj
    Replies:
    5
    Views:
    395
Loading...

Share This Page