Handing a number of methods to the same child class

Dave Merrill dmerrillq at usaq.netq
Tue Jan 11 01:09:06 CET 2005


Python newb here. Say a class contains some rich attributes, each defined as
a class. If an instance of the parent class recieves a call to a method
belonging to one of those attributes, it should be dispatched to the
corresponding child class.

Somewhat silly example:

class Address:
    def __init__():
        self.displayed_name = ''
        self.adr = ''
        self.city = ''
        self.state = ''
    def set_name(name):
        self.displayed_name = name
    def set_adr(adr):
        self.adr = adr
    def set_city(city):
        self.city = city
    def set_state(state):
        self.state = state

class Phone:
    def __init__():
        self.displayed_name = ''
        self.number = ''
    def set_name(name):
        self.displayed_name = name
    def set_number(number):
        self.number = number

class Customer:
    def __init__():
        self.last_name = ''
        self.first_name = ''
        self.adr = Adr()
        self.phone = Phone()
    def set_adr_name(name):
        self.adr.set_name(name)
    def set_adr_adr(adr):
        self.adr.set_adr(adr)
    def set_adr_city(city):
        self.adr.set_city(city)
    def set_adr_state(state):
        self.adr.set_state(state)
    def set_phone_name(name):
        self.phone.set_name(name)
    def set_phone_number(number):
        self.phone.set_number(number)

IOW, all the adr methods go to the corresponding method in self.adr, all the
phone methods go to self.phone, theorectically etc for other rich
attributes.

What I'd really like is to say, "the following list of methods pass all
their arguments through to a method of the same name in self.adr, and the
following methods do the same but to self.phone." Is there some sane way to
express that in python?

Callers should stay ignorant about the internal structure of customer
objects; they should be talking only to the customer object itself, not its
components. Customer objects should stay ignorant of the internal structure
of addresses and phones; they should let those objects handle their own
implementation of the methods that apply to them.

What customer objects need to do is call the appropriate internal object for
each incoming method. How would you implement this? It's unfortunate to have
to create individual passthrough methods for everything, like the above,
among other reasons because it makes customer objects have to know about
each new method implemented by the objects they contain.

Am I making sense? Thanks,

Dave Merrill





More information about the Python-list mailing list