Message from discussion
best way to code
Path: g2news2.google.com!postnews.google.com!r24g2000vbp.googlegroups.com!not-for-mail
From: miya <nmiyas...@gmail.com>
Newsgroups: comp.lang.python
Subject: Re: best way to code
Date: Sat, 20 Dec 2008 16:26:25 -0800 (PST)
Organization: http://groups.google.com
Lines: 80
Message-ID: <041d4bd9-c847-4130-8188-1817908f83dc@r24g2000vbp.googlegroups.com>
References: <f2ebf11e-d3e6-49e5-9865-91a31a6df34a@z6g2000pre.googlegroups.com>
<gigifr$rl1$00$1@news.t-online.com>
NNTP-Posting-Host: 201.250.122.147
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1229819185 10357 127.0.0.1 (21 Dec 2008 00:26:25 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Sun, 21 Dec 2008 00:26:25 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: r24g2000vbp.googlegroups.com; posting-host=201.250.122.147;
posting-account=scJwhwoAAADXWQRuGqNM8vyqt63Ix1Si
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5)
Gecko/2008120121 Firefox/3.0.5,gzip(gfe),gzip(gfe)
On Dec 19, 2:35=A0pm, Peter Otten <__pete...@web.de> wrote:
> eric wrote:
> > hi,
>
> > I need to find a "good" design pattern to instanciate, and add
> > specific code all in one. Let me explain it :
>
> > I need to define "some" code, better be in a class, something like
>
> > class LinkA(object):
> > =A0 =A0 def mystuff(self):
> > =A0 =A0 =A0 =A0 =A0<do something different>
>
> > class LinkB(object):
> > =A0 =A0 def mystuff(self):
> > =A0 =A0 =A0 =A0 =A0<do something different again>
>
> > AND I need an instance of this class
> > { "stuff A": LinkA()
> > =A0 "stuff B": LinkB()
> > }
>
> > This kind of code "would" be fine, I mean, the result effect in memory
> > is fine for me.
> > But I don't like the way I have to
> > 1/ give a useless name to LinkA, linkB (there can be hundreds of names
> > like that)
> > 2/ I have to write it down two times (and that's one time too much)
>
> > any ideas ?
>
> > something like
> > [
> > new object():
> > =A0 =A0 def mystuff(self):
> > =A0 =A0 =A0 =A0<do something>
> > ,
> > new object():
> > =A0 =A0 def mystuff(self):
> > =A0 =A0 =A0 =A0<do something else>
> > ]
>
> > would be really perfect (but I know it does not work, or at least, I
> > don't know how to make it work)
>
> > In fact, I would like to define a class, and an instance in a single
> > statement
> >>> class Register:
>
> ... =A0 =A0 def __init__(self):
> ... =A0 =A0 =A0 =A0 =A0 =A0 self.items =3D []
> ... =A0 =A0 def __call__(self, method):
> ... =A0 =A0 =A0 =A0 =A0 =A0 class Link(object):
> ... =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 mystuff =3D method
> ... =A0 =A0 =A0 =A0 =A0 =A0 self.items.append(Link())
> ...>>> register =3D Register()
> >>> @register
>
> ... def mystuff(self): print "first"
> ...>>> @register
>
> ... def mystuff(self): print "second"
> ...>>> for item in register.items:
>
> ... =A0 =A0 item.mystuff()
> ...
> first
> second
>
> Peter
Wow, loved this solution. Never thought about using decorators to
solve this kinda problems.
nice
-
Nicol=E1s Miyasato (miya)
http://myPythonNotes.wordpress.com
http://nmiyasato.blogspot.com