[Python-3000] i guess i was misunderstood
Josiah Carlson
jcarlson at uci.edu
Thu May 18 00:40:34 CEST 2006
"Robin Bryce" <robinbryce at gmail.com> wrote:
> In the python tutorial 'self' is introduced like this: "the special thing
> about methods is that the object is passed as the first argument of the
> function". And ever since reading that I've always expected to be able to
> treat 'self' as just another parameter. Talins' first example illustrates
> that 'self' can not be treated this way.
>
> If there was a way to allow `A.somemethod(otherself)` where
> `isinstance(otherself, A) is False`, as a language feature, then I would
> find my self reaching for things like PyProtocols less often. I'm not
> knocking PyProtocols, I just wish there was a middle ground available that
> didn't nobble future adoption (no pun intended) of PyProtocols et al.
One could use the @staticmethod decorator, but then one would need to
pass the instance every time.
Generally speaking, I like that Python enforces type when using unbound
methods; it has saved me from some foolish bugs. Also, the cases where
I have generally wanted the "first argument" to be anything that
supports a particular interface, I generally don't make them methods of
a class.
I would suggest that code which uses an unbound method on an object
unrelated to the class is actually trying to be too clever; make it a
plain function or a static method.
- Josiah
More information about the Python-3000
mailing list