John Salerno wrote:
> Ok, I have a new random question for today -- feel free to ignore and
> get back to your real jobs! :)
> 
> Anyway, I'm creating a GUI (yes, all part of my master plan to
> eventually have some sort of database application working) and it's
> going to involve a wx.Notebook control. I think I have two options for
> how I can do this. Within the class for the main window frame, I can say:
> 
> notebook = wx.Notebook(panel) # panel is parent of the Notebook control
> 
> This uses the default wx.Notebook class, and works just fine. But I was
> thinking, is it a smart idea to do this instead:
> 
> class MyNotebook(wx.Notebook):
>     def __init__(self, parent):
>         wx.Notebook.__init__(self, parent)
> 
> and then call it from the main frame as:
> 
> notebook = MyNotebook(panel)
> 
> This seems to allow for future expansion of the customized Notebook
> class, but at the same time I have no idea how or why I'd want to do that.
> 
> So my question in general is, is it a good idea to default to an OOP
> design like my second example when you aren't even sure you will need
> it?

It of course depends on a lot of factors. Two of these factors are:

1/ given my current knowledge of the project, what are the probabilities
that I'll end up subclassing wx.Notebook ?

2/ if so, in how many places would I have to s/wx.Notebook/MyNotebook/

The second factor is certainly the most important here. Even if the
answer to 1/ is "> 50%", if there's only a couple of calls in a single
file, there's really no need to do anything by now IMHO.

As a side note, the common OO pattern for this potential problem is to
replace direct instanciation with a factory, so you just have to modify
the factory's implementation.

Now one of the nice things with Python is that it doesn't have a "new"
keyword, instead using direct calls to the class (the fact is that in
Python, classes *are* factories already). Another nice thing is that you
can easily 'alias' callables. The combination of these 2 features makes
factory pattern mostly straightforward and transparent. As someone
already pointed out, you don't need to subclass wx.Notebook - just
'alias' it to another name.

> I know it won't hurt, 

Mmm... Not so sure. One could argue that "premature generalization is
the root of all evil" !-)

> and is probably smart to do sometimes,

cf above.

> but
> maybe it also just adds unnecessary code to the program.

It's not that much about the unnecessary code (which can boil down to a
single assignement), but about the unnecessary level of indirection
(which, as someone stated, is the one and only problem that cannot be
solved by adding a level of indirection !-).



-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to