[Newbie] design question

Daniel Nogradi nogradi at gmail.com
Sat May 12 14:17:17 EDT 2007


> Suppose I have class ShoppingCart which has one method called buy(),
> and class Buyer who has one reference to ShoppingCart...  Can I also
> have method buy() in class Buyer, which will then called
> ShoppingCard.buy(), and also do some other stuff?  Is this legal
> design pattern, have methods with same name?

Yes, something like this is perfectly fine.

class ShoppingCart( object ):
    def buy( self ):
        print 'buy in shoppingcart'

class Buyer( object ):
    def __init__( self, cart ):
        self.cart = cart

    def buy( self ):
        print 'buy in buyer'
        self.cart.buy( )
        print 'some extra stuff'

acart = ShoppingCart( )
abuyer = Buyer( cart=acart )
abuyer.buy( )


HTH,
Daniel



More information about the Python-list mailing list