reaching hidden methods + casting

P

per9000

Hi,
can I reach a hidden method when doing ugly inheritance in python?
.... def spin(self, n): print "A", n
........ def spin(self, m): print "B", m
........ def spin(self, k): print "C", k
....['__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 ';'

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?1337

So is there another way of digging into the past of a class? Or can/
should I create constructors for the classes A, B and C that takes
objects of the other classes?

Or should I have thought about getting unique names before I
implemented the ugly inheritance graph?

/Per
 
G

Gabriel Genellina

Hi,
can I reach a hidden method when doing ugly inheritance in python?
... def spin(self, n): print "A", n
...... def spin(self, m): print "B", m
...... def spin(self, k): print "C", k
...['__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 ';'

Try this in Python:
B.spin(myC, "Lancelot")

You can't ask the instance for myC.spin because that would retrieve
C.spin; you need B.spin instead. But if you get it this way, it's not
associated to a specific instance, so you must pass myC explicitely
(becoming 'self').
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?

Yes.

So is there another way of digging into the past of a class? Or can/
should I create constructors for the classes A, B and C that takes
objects of the other classes?

No need for that. And usually, that's not what you want either: you're
creating a *different* object that way, not calling a (shadowed) method on
an existing object.
Or should I have thought about getting unique names before I
implemented the ugly inheritance graph?

Perhaps...
 
B

Bruno Desthuilliers

per9000 a écrit :
Hi,
can I reach a hidden method when doing ugly inheritance in python?
... def spin(self, n): print "A", n
...... def spin(self, m): print "B", m
...... def spin(self, k): print "C", k
...['__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?
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.
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top