473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

reaching hidden methods + casting

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 ';'

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

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

--

Per Erik Strandberg
blog: http://www.pererikstrandberg.se/blog/

Apr 12 '07 #1
3 1526
En Thu, 12 Apr 2007 04:18:19 -0300, per9000 <pe*****@gmail.comescribió:
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 ';'
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?
>>>leet = int('1337')
leet
1337
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...

--
Gabriel Genellina

Apr 12 '07 #2
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.

Apr 12 '07 #3
On 12 Apr, 09:42, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
>
<snip>
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")
<snip>

Thanks, that was exactly the insight i needed.

/Per

--

Per Erik Strandberg
blog: http://www.pererikstrandberg.se/blog/

Apr 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sam Sungshik Kong | last post by:
Hello! While using panel control, I wondered a thing. Panel class is subclass of Control class. Control class has KeyPress event and Focus() method, etc... Then Panel class must have them. I...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
6
by: Verbal Kint | last post by:
hi. could you please have a look to the following code: class cClassA { public: cClassA(void):itsX(0) {} void SetX (int val) {itsX = val;}
7
by: newbie | last post by:
hi, I have the following construct: public class Form1:System.Windows.Forms.Form { ..... ----a TextBox is created by the Designer in this area: 'textBox1' ..... }
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
3
by: Steve | last post by:
Hi; I'm working on a demo of using a timer on a web site that is made visible by making a div visible. My "PopIn Box" div is empty on the page. Before making it visible I used javascript to...
12
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
4
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have written a generic method which does different things depending on the type of the parameter. I got it to work, but it seems really inelegant. Is there a better way to do this? In the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.