473,770 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python's OOP question

There's a program, it's result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?

Expand|Select|Wrap|Line Numbers
  1.  
  2. class C1(object):
  3. def v(self, o):
  4. return "expected "+o
  5.  
  6. class C2(object):
  7. def v(self, o):
  8. return "unexpected "+o
  9. def m(self):
  10. print self.v("aaa")
  11.  
  12. class C3(object):
  13. def nothing(self):
  14. pass
  15.  
  16. def test1():
  17. o = C3()
  18. setattr(o,"m",C2().m)
  19. setattr(o,"v",C1().v)
  20. o.m()
  21.  
  22. test1()
  23.  
  24.  
Oct 16 '06 #1
23 1514
neoedmund wrote:
There's a program, it's result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?

Expand|Select|Wrap|Line Numbers
  1. class C1(object):
  2.     def v(self, o):
  3.         return "expected "+o
  4. class C2(object):
  5.     def v(self, o):
  6.         return "unexpected "+o
  7.     def m(self):
  8.         print self.v("aaa")
  9. class C3(object):
  10.     def nothing(self):
  11.         pass
  12. def test1():
  13.     o = C3()
  14.     setattr(o,"m",C2().m)
  15.     setattr(o,"v",C1().v)
  16.     o.m()
  17. test1()
  18.  
class C3(C1, C2):pass
>>C3.mro() # shows method resolution order
[<class '__main__.C3'>, <class '__main__.C1'>, <class '__main__.C2'>,
<type 'object'>]
>>o = C3()
o.m()
expected aaa

Oct 16 '06 #2
"neoedmund" <ne*******@gmai l.comwrites:
There's a program, it's result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?

[code]

class C1(object):
def v(self, o):
return "expected "+o

class C2(object):
def v(self, o):
return "unexpected "+o
def m(self):
print self.v("aaa")

class C3(object):
def nothing(self):
pass

def test1():
o = C3()
setattr(o,"m",C 2().m)
setattr(o,"v",C 1().v)
o.m()
Setting attributes on an object externally isn't the same thing as
making bound methods of that object.

In this case, 'o.m' is a bound method of a C2 instance, and has no
knowledge of C1. 'o.v' is a bound method of a C1 instance, and has no
knowledge of C2. Neither of them has any knowledge of C3.

What is it you're trying to achieve?

--
\ "Unix is an operating system, OS/2 is half an operating system, |
`\ Windows is a shell, and DOS is a boot partition virus." -- |
_o__) Peter H. Coffin |
Ben Finney

Oct 16 '06 #3
thank you, Kay.

But i need a "dynamic" way. Say i have a existing class, and add some
method from other class into it.
Kay Schluehr wrote:
neoedmund wrote:
There's a program, it's result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  class C1(object):
  3.      def v(self, o):
  4.          return "expected "+o
  5.  
  6.  class C2(object):
  7.      def v(self, o):
  8.          return "unexpected "+o
  9.      def m(self):
  10.          print self.v("aaa")
  11.  
  12.  class C3(object):
  13.      def nothing(self):
  14.          pass
  15.  
  16.  def test1():
  17.      o = C3()
  18.      setattr(o,"m",C2().m)
  19.      setattr(o,"v",C1().v)
  20.      o.m()
  21.  
  22.  test1()
  23.  
  24.  

class C3(C1, C2):pass
>C3.mro() # shows method resolution order
[<class '__main__.C3'>, <class '__main__.C1'>, <class '__main__.C2'>,
<type 'object'>]
>o = C3()
o.m()
expected aaa
Oct 16 '06 #4
I'm trying to achieve a higher level of "reusabilit y". Maybe it cannot
be done in python? Can anybody help me?
Ben Finney wrote:
"neoedmund" <ne*******@gmai l.comwrites:
There's a program, it's result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?

[code]

class C1(object):
def v(self, o):
return "expected "+o

class C2(object):
def v(self, o):
return "unexpected "+o
def m(self):
print self.v("aaa")

class C3(object):
def nothing(self):
pass

def test1():
o = C3()
setattr(o,"m",C 2().m)
setattr(o,"v",C 1().v)
o.m()

Setting attributes on an object externally isn't the same thing as
making bound methods of that object.

In this case, 'o.m' is a bound method of a C2 instance, and has no
knowledge of C1. 'o.v' is a bound method of a C1 instance, and has no
knowledge of C2. Neither of them has any knowledge of C3.

What is it you're trying to achieve?

--
\ "Unix is an operating system, OS/2 is half an operating system, |
`\ Windows is a shell, and DOS is a boot partition virus." -- |
_o__) Peter H. Coffin |
Ben Finney
Oct 16 '06 #5
[Please don't top-post above the text to which you're replying.]

"neoedmund" <ne*******@gmai l.comwrites:
I'm trying to achieve a higher level of "reusabilit y". Maybe it
cannot be done in python? Can anybody help me?
What, specifically, are you trying to achieve? What problem needs
solving?

--
\ "If you're a horse, and someone gets on you, and falls off, and |
`\ then gets right back on you, I think you should buck him off |
_o__) right away." -- Jack Handey |
Ben Finney

Oct 16 '06 #6
python use multiple inheritance.
but "inheritanc e" means you must inherite all methods from super type.
now i just need "some" methods from one type and "some" methods from
other types,
to build the new type.
Do you think this way is more flexible than tranditional inheritance?
Ben Finney wrote:
[Please don't top-post above the text to which you're replying.]

"neoedmund" <ne*******@gmai l.comwrites:
I'm trying to achieve a higher level of "reusabilit y". Maybe it
cannot be done in python? Can anybody help me?

What, specifically, are you trying to achieve? What problem needs
solving?

--
\ "If you're a horse, and someone gets on you, and falls off, and |
`\ then gets right back on you, I think you should buck him off |
_o__) right away." -- Jack Handey |
Ben Finney
Oct 16 '06 #7
neoedmund schrieb:
python use multiple inheritance.
but "inheritanc e" means you must inherite all methods from super type.
now i just need "some" methods from one type and "some" methods from
other types,
to build the new type.
Do you think this way is more flexible than tranditional inheritance?
Probably your problem is better solved with delegation instead of
inheritance.

--
Servus, Gregor
Oct 16 '06 #8
neoedmund wrote:
python use multiple inheritance.
but "inheritanc e" means you must inherite all methods from super type.
now i just need "some" methods from one type and "some" methods from
other types,
to build the new type.
Do you think this way is more flexible than tranditional inheritance?
The following does the trick:

from types import MethodType

def addMethod(meth, obj):
f = meth.im_func
setattr(obj, f.__name__, MethodType(f,ob j))

def test1():
addMethod(C2.m, C3)
addMethod(C1.v, C3)
o = C3()
o.m()

The same works as is on modifying individual instances, rather than
their class:

def test2():
o = C3()
addMethod(C2.m, o)
addMethod(C1.v, o)
o.m()
# raises AttributeError
# C3().m()
George

Oct 16 '06 #9
I found a dynamic way to inherite classes:

def MixIn(pyClass, mixInClass):
if mixInClass not in pyClass.__bases __:
pyClass.__bases __ += (mixInClass,)

def test1():
o = C3()
MixIn(C3,C1)
MixIn(C3,C2)
o.m()

"expected aaa"

neoedmund wrote:
thank you, Kay.

But i need a "dynamic" way. Say i have a existing class, and add some
method from other class into it.
Kay Schluehr wrote:
neoedmund wrote:
There's a program, it's result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?
>
Expand|Select|Wrap|Line Numbers
  1.  >
  2.  class C1(object):
  3.      def v(self, o):
  4.          return "expected "+o
  5.  >
  6.  class C2(object):
  7.      def v(self, o):
  8.          return "unexpected "+o
  9.      def m(self):
  10.          print self.v("aaa")
  11.  >
  12.  class C3(object):
  13.      def nothing(self):
  14.          pass
  15.  >
  16.  def test1():
  17.      o = C3()
  18.      setattr(o,"m",C2().m)
  19.      setattr(o,"v",C1().v)
  20.      o.m()
  21.  >
  22.  test1()
  23.  >
  24.  
class C3(C1, C2):pass
>>C3.mro() # shows method resolution order
[<class '__main__.C3'>, <class '__main__.C1'>, <class '__main__.C2'>,
<type 'object'>]
>>o = C3()
>>o.m()
expected aaa
Oct 16 '06 #10

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

Similar topics

44
3422
by: Carl | last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O" http://www.osnews.com/story.php?news_id=5602 I think this is an unfair comparison! I wouldn't dream of developing a numerical application in Python without using prebuilt numerical libraries and data objects such as dictionaries and lists. I have been experimenting with numerical algorithms in Python with a heavy use of the Numeric module. My experience is that Python...
114
9884
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
68
5896
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
50
5737
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
112
13864
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them because they smell "Javaish." But now my code base is expanding and I'm beginning to appreciate the wisdom behind them. I welcome example code and illustrations.
158
6424
by: Giovanni Bajo | last post by:
Hello, I just read this mail by Brett Cannon: http://mail.python.org/pipermail/python-dev/2006-October/069139.html where the "PSF infrastracture committee", after weeks of evaluation, recommends using a non open source tracker (called JIRA - never heard before of course) for Python itself. Does this smell "Bitkeeper fiasco" to anyone else than me? --
4
2128
by: Martitza | last post by:
Hi. I work for a small company (actually in process of forming) interested in embedding or extending python as part of our commercial non-open-source product. We have legal counsel, but are interested in the spirit as well as the letter of the law. Not much seems to have been written about the python license since version 2, so pointers to more recent discussions or contacts are appreciated. If this is not the right place to ask these...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.