group several methods under a attribute
Rhodri James
rhodri at wildebst.demon.co.uk
Mon Apr 6 18:56:36 EDT 2009
On Mon, 06 Apr 2009 20:52:50 +0100, jelle <jelleferinga at gmail.com> wrote:
> Hi Aaron,
>
> Thanks a lot for your suggestions.
> I wasnt familiar with the __get__ magic, which seems interesting.
>
> So, finally it seems that the cleanest pattern is:
>
> class ClsA( object ):
> def __init__( self, other ):
> self.inst= other
>
> def submethA( self, arg ):
> print( 'submethA %r, instance %r'% ( arg, self.inst ) )
>
> class ClsB( object ):
> def methA( self, arg ):
> self.A= ClsA( self )
> print( 'methA %r'% arg )
>
> b= ClsB( )
> b.methA( 'this' )
> b.A.submethA( 'that' )
You'll note that this is, all Aaron's protests to the contrary,
splitting your class up into multiple cooperating classes. If
you're set on doing it like this, doing it this way avoids
polluting your namespace so much:
class ClsB(object):
class ClsA(object):
def do_something(self):
print "Here's A doing something"
def __init__(self):
self.A = ClsB.ClsA()
def do_something(self):
print "Here's B doing something"
b = ClsB()
b.do_something()
b.A.do_something()
--
Rhodri James *-* Wildebeeste Herder to the Masses
More information about the Python-list
mailing list