Carolyn Johnston (MSNAR) wrote:
Very good. I tried this with the following example and got the kind of behavior
I wanted. Thanks, everyone.
class A:
def __init__(self):
pass
def f(self):
print "A.f"
return
class B(A):
# no init method, inherit the A one
def f(self):
print "B.f"
return
class C(A):
def f(self):
print "C.f"
return
from testClasses import *
cc = C()
cc.f()
C.f
A.f(cc)
A.f
B.f(cc)
Traceback (most recent call last):
File , line 0, in <stdin>##188
TypeError: unbound method f() must be called with B instance as first argument
(got C instance instead)
That's because cc is not an instance of B.
Try:
class C(A, B):
....
Michael
Carolyn Johnston (carolj)
Lead Researcher, MSN Applied Research Text group
Bldg 109/4053 | MIcrosoft Corporation
425-706-2153
-----Original Message-----
From: users-boun...@lists.ironpython.com
[mailto:users-boun...@lists.ironpython.com] On Behalf Of Michael Foord
Sent: Friday, March 27, 2009 11:29 AM
To: Discussion of IronPython
Subject: Re: [IronPython] .NET casts in IronPython
Carolyn Johnston (MSNAR) wrote:
You are trying to apply C++ and C# thinking to Python. This is a
fundamental philosophical difference between the languages. Python
doesn't care what the object IS. Python only cares what it EXPOSES. If
it has a GetName() method, you can call it, no matter what the object
ancestry was. -- tim
Well, C++ was my first language -- but I am a huge fan of Python and
IronPython. Since I've dived into the deep end philosophically, I may as well
push it a bit further.
One of the most useful aspects of IronPython for me is the way that I can use IronPython as a loosely typed test environment for .NET classes that I've built in C#. Suppose I have a class B which is a subclass of A, which both define a function f(x). Suppose I have an object of class B on which (for some purely hypothetical reason) I want to run A.f(x) rather than B.f(x).
Actually the example pattern is pretty much how you do it.
Suppose you have an object 'a', which is an instance of a class that
inherits from (or implements) both A and B.
You can explicitly call the A version using:
A.f(a, x)
Or the B version:
B.f(a, x)
As you are calling the unbound method (on the class or interface), you
pass in the instance as the first argument.
Michael
This is clearly at the interface of .NET design and Pythonian antiObjectian
philosophy. Here's my question: is it doable within IronPython, or is it not?
And if so, how?
Thanks,
:) Carolyn
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
--
http://www.ironpythoninaction.com/
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com