reaching hidden methods + casting
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Apr 12 04:52:16 EDT 2007
per9000 a écrit :
> Hi,
> can I reach a hidden method when doing ugly inheritance in python?
>
>>>> class A:
> ... def spin(self, n): print "A", n
> ...
>>>> class B:
> ... def spin(self, m): print "B", m
> ...
>>>> class C(A,B):
> ... def spin(self, k): print "C", k
> ...
>>>> myC = C()
>>>> dir(myC)
> ['__doc__', '__module__', 'spin']
>
> In f.x. the C-family of languages I guess something like this would
> call B.spin:
> ((B)myC).spin("Lancelot"); // almost forgot the ';'
B.spin(myC, "lancelot")
In Python, the syntax:
some_instance.some_method(param)
is syntactic sugar for
SomeClass.some_method(some_instance, param)
(assuming isinstance(some_instance, SomeClass) == True)
> Please correct me I am wrong (which I likely am) but as I understand
> it this example calls the constructor of int instead of casting it,
> right?
>>>> leet = int('1337')
>>>> leet
> 1337
There's nothing like "casting" in Python - it would be meaningless in a
dynamically typed language. The above example does not "cast" a string
to an int, it creates an int - you have two distinct objects, whereas
with casting you have two representations of the same object.
> Or should I have thought about getting unique names before I
> implemented the ugly inheritance graph?
This is a design question, and we don't have enough context to answer it.
More information about the Python-list
mailing list