[Python-3000] i guess i was misunderstood
tomer filiba
tomerfiliba at gmail.com
Thu May 11 19:27:25 CEST 2006
"Guido van Rossum" <guido <at> python.org> wrote
> You seem to be still under the influence of the type-safety mafia, who
> want to make you believe that with enough type-checking you can
> prevent all bugs (though they don't believe it themselves).
~~
>> I think that I agree with the gist of Tomer's point.
>Well, to most of us it's far from clear what he's trying to say...
no, i'm quite the opposite in fact. i want a type-less language. sure,
builtin types like int/str/float need some concrete distinction, but apart
from those, all language-level objects should be typeless.
and python is half way there really. a type in python is basically the
object's MRO, and most code relies to the *attributes* of the object
other than it's type., i.e., "for" loops requires __iter__ instead of
inheritence like in java.
which is all good.
--------
but, the usage of isinstance(x, y) or type(x) should be deprecated
or at least limited only to where it's *needed*, for example:
class A(object):
def __init__(self, x):
self.x = x
class B(object):
def __init__(self):
A.__init__(self, 5)
b=B() # error, A.__init__ expects an instanc of A
why is this kind of type-checking enforced by the language? i wanted to
create class proxies, so i could inherit from instances. for example:
class myFrame(derive_of(c.modules.wx.Frame)):
...
c.modules.wx.Frame is a proxy (instance) to a remote type, so you can't
derive from it directly. therefore, derive_of is
def derive_of(proxy_type):
class cls(object):
def __getattr__(self, name):
return getattr(proxy_type, name)
return cls
which basically means that when the attribute doesn't belong to the
instance of local class, it is queried for at the remote class. but it doesnt
work, because the method checks for the instance's type.
(the real code was quite more complex, and the above snippet is only
an example of the concept, not production code)
what this code does is basically changing the __mro__ of the object. had
__mro__ been mutable, it would all be simpler.
-------
yes, i understand python suddenly dropping types is a very drastic change
that isn't likely to happen. so i say, keep the types, just don't count on them
too much. just use duck-typing. AttributeError is equivalent to TypeError.
and as i showed in the "interfaces" example, you can't rely on inheritence
to determine if some operation is going to be valid. python is not java, as it
doesnt enforce conformness to method signatures, thus relying on
isinstance() is not really gonna help you -- as i showed, the derived class
can break the interface's signatures.
therefore, the only thing you can count on is ducktyping, which is basically
querying the object if it has the relevant attributes, not the relevant base
class.
relying on hasattr rather than on isinstance would mean proxies would
work just fine.
the other sollution i proposed was adding a __type__() special method
that returns the object's type for type(x) or isinstance(x,y) --
class Prxoy(object):
def __init__(self, obj):
self.__real_obj = obj
def __getattr__(self, name):
return getattr(self.__real_obj, name)
def __type__(self):
"""returns the 'real type' of the proxy"""
return type(self.__real_obj)
p = Proxy(5)
print p.__class__
# <class __main__.Proxy>
print type(p)
# <type int>
print p+4 # --> p.__add__(4) --> p.__real_obj.__add__(4)
# 9
(by default, __type__() just returns __class__)
-------
i really hope i made my point clear now. i want object proxies to be
transparent to the language. this can either be done by not depending
on the "real" type of objects, i.e., using hasattr instead of isinstance,
or adding the new special method i suggested, or some similar mechanism.
-tomer
More information about the Python-3000
mailing list