(Easy ??) question about class definition

Gregoire Welraeds greg at perceval.be
Thu Mar 2 04:43:28 EST 2000


In reply to the message of Quinn Dunkan sent on Mar 1 (see below) :

> Hmm, well, I've never read anything on design patterns (should though, any book
> recommendations?)

Have a look at: 
www.hillside.net/patterns

And in the book section of the site, look  for:
Design Patterns: Elements of Reusable Object-Oriented Software. Gamma
et. al.
(THE reference :)

--
Life is not fair
But the root password helps
--

Gregoire Welraeds
greg at perceval.be
Perceval Development team
-------------------------------------------------------------------------------
Perceval Technologies sa/nv	Tel: +32-2-6409194		
Rue Tenbosch, 9			Fax: +32-2-6403154		
B-1000 Brussels			general information:   info at perceval.net
BELGIUM				technical information: helpdesk at perceval.net
URL: http://www.perceval.be/
-------------------------------------------------------------------------------

On 1 Mar 2000, Quinn Dunkan wrote:

> Date: 1 Mar 2000 18:56:49 GMT
> From: Quinn Dunkan <quinn at drachma.ugcs.caltech.edu>
> To: python-list at python.org
> Newsgroups: comp.lang.python
> Subject: Re: (Easy ??) question about class definition
> 
> On Tue, 29 Feb 2000 18:13:37 +0100 (CET), Gregoire Welraeds <greg at perceval.be>
> wrote:
> >Hello again,
> >
> >I think the problem I have is common in OOP... but I don't have any
> >beginning of solution.
> >
> >I got the following problem. Imagine i want to manage a menu. So I define
> >2 classes. First one is the class menu and is basicly a list of entry. The
> >second is the item class representing each menu entry. When an item of my
> >menu is selected, I want to execute some action... The action I want to
> >perform is different for each item. So I get the following
> >
> >class entry:
> >	def __init__(self, name, action):
> >		self.name= name
> >		self.action= action
> >	def selected():
> >		exec(self.action)
> >	[some methods]
> >
> >class menu:
> >	def __init__(self, name):
> >		self.name= name
> >		self.list=[]
> >	[some methods]
> >...
> >
> >Now self.action is a reference to something I have to define elsewhere. It
> >could be a separate function... but then, the function is not bound to a
> >class as method are and everybody can use it. And I don't want to have
> >each action to be define in the class. 
> >thus the problem is that each action i bound with an instance of the class
> >item.
> >I know there are some solutions in Design Pattern but I don't know howto
> >implement this in Python.
> 
> Hmm, well, I've never read anything on design patterns (should though, any book
> recommendations?), but why not just make entry an abstract base?  E.g.:
> 
> class MenuEntry:
>     name = 'no name'
>     def __init__ etc.
>     def selected(self):
>         say 'there is no action associated with this entry'
>         (or maybe self.grayed_out = 1 in __init__)
> 
> class RescanArchive(MenuEntry):
>     name = 'Rescan CIA Archive...'
>     def selected(self):
>         etc.
> 
> 
> I haven't done much with Tkinter or other gui toolkits, but I know Tkinter at
> least uses a lot of callbacks, which implies it's closer to your approach.
> 
> And I imagine your approach is constrained anyway but what toolkit you're
> using.
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 
> 





More information about the Python-list mailing list