Type question

N

nitrogenycs

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
 
S

Serge Orlov

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top