473,662 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handing a number of methods to the same child class

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(numb er):
self.number = number

class Customer:
def __init__():
self.last_name = ''
self.first_name = ''
self.adr = Adr()
self.phone = Phone()
def set_adr_name(na me):
self.adr.set_na me(name)
def set_adr_adr(adr ):
self.adr.set_ad r(adr)
def set_adr_city(ci ty):
self.adr.set_ci ty(city)
def set_adr_state(s tate):
self.adr.set_st ate(state)
def set_phone_name( name):
self.phone.set_ name(name)
def set_phone_numbe r(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
Jul 18 '05 #1
1 1341
Dave Merrill wrote:
Somewhat silly example:
I know you've hedged this by calling it a "silly" example, but I would
like to point out that your set_X methods are unnecessary -- since
Python allows you to overload attribute access, getters and setters are
generally unnecessary.
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(numb er):
self.number = number

class Customer:
def __init__():
self.last_name = ''
self.first_name = ''
self.adr = Adr()
self.phone = Phone()
def set_adr_name(na me):
self.adr.set_na me(name)
def set_adr_adr(adr ):
self.adr.set_ad r(adr)
def set_adr_city(ci ty):
self.adr.set_ci ty(city)
def set_adr_state(s tate):
self.adr.set_st ate(state)
def set_phone_name( name):
self.phone.set_ name(name)
def set_phone_numbe r(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?


py> class Address(object) :
.... def __init__(self, city, state):
.... self.city = city
.... self.state = state
....
py> class Customer(object ):
.... def __init__(self, name, addr):
.... self.name = name
.... self.addr = addr
.... def __getattr__(sel f, attr):
.... if attr.startswith ('adr_'):
.... return getattr(self.ad dr, attr[4:])
.... raise AttributeError( attr)
....
py> c = Customer("Steve ", Address("Tucson ", "AZ"))
py> c.adr_city
'Tucson'
py> c.adr_state
'AZ'

I've used a slightly different example from yours, but hopefully you can
see how to apply it in your case. The __getattr__ method is called when
an attribute of an object cannot be found in the normal locations (e.g.
self.__dict__). For all attributes that begin with "adr_", I delegate
the attribute lookup to the self.addr object instead.

Steve
Jul 18 '05 #2

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

Similar topics

8
2335
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the number 2. I actually wrote a java function that did this a couple of years ago, in school, as an excersise in "binary trees". I lost it, and most of my programming knowledge, but I figured perhaps there is a way to do this easily in python? It...
14
6397
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this is the right and common case to call init: class Mother(object): def __init__(self, param_mother): print 'Mother'
2
1635
by: Thomas Matthews | last post by:
Hi, In pursuit of a child class blocking a parent's virtual method, I came up with this example: #include <iostream> #include <cstdio> using namespace std; class Grandparent
9
5117
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In half-code this should in the end look like this: In the child class:
2
4214
by: Andrea Gavana | last post by:
Hello NG, this may seem a stupid (or even impossible) question, but my knowlegde of Python is quite limited. I have basically a simple graphical user interface that contains a Panel, another panel (child of the main panel) and a custom widget (child of the main panel). Basically is something like (only some small code, is in wxPython but the question is more Python-related): class MainClass(wx.Panel):
3
1180
by: Peteroid | last post by:
Is it possible to make a public parent class method unavailable (i.e., generate an error at compile time) to a particular child class? For example, say a parent class has a public method Add( ). I want to create a child class of this parent class that does not have an Add( ) method (while possibly another child class does). I think I figured it out while writing this, so tell me if this is the 'standard method'. Make the parent class...
8
1638
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml, dictionary, etc. I was wondering if I can use decorators to accomplish this. For instance, I have the following method def getUsers(self, params): return users.query(dbc)
1
6525
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the Talker() in Parent will call the Talker() method in every child class. The kicker is that I have many different Child classes, and not all Child classes will be loaded when Talker() in the Parent is called. Thanks, Randy
17
1684
by: Donn Ingle | last post by:
Hi, Here's a framework for the questions: --- In a module, part of an API --- class Basis ( object ): def foo ( self, arg ): pass --- In user's own code --- class Child ( Basis ):
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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
4179
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
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
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.