From: Stephen Hansen on 28 Mar 2010 00:31 On 2010-03-26 07:49:02 -0700, kj said: > What's the word on using "classes as namespaces"? E.g. > > class _cfg(object): > spam = 1 > jambon = 3 > huevos = 2 > > breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos) > > > Granted, this is not the "intended use" for classes, and therefore > could be viewed as a misuse ("that's what dictionaries are for", > etc.). But other than this somewhat academic objection[*], I really > can see no problem with using classes in this way. On the contrary, this is the intended use of classes. Or at least, one of them. A class *is* a namespace, albeit one that you must address explicitly unlike the local and global namespaces which are usually implicit. That said... > [*] My own subjective dislike for the widespread practice of using > triple quotes to comment out code is formally similar to this one > ("the 'intended use' for triple-quoting is not to comment out code", > etc.). Here I find myself on the opposite side of the purist/pragmatic > divide. Hmmm. What?! Where do you get this "widespread practice"? You mentioned that before when you last posted about that and I forgot to comment. I've never seen it. In the 110k lines of in-house code I maintain, we don't use it once; we have somewhere around 300k lines of third-party code from a wide range of sources, and although I haven't reviewed it all by any means, I regularly have to peek over it and I never seen triple quoted "comments". Hell, I almost never see commented -code-. Code should only be commented while fiddling or debugging. Once fiddlng is done, dead code should be removed. I'm sure it -happens- every once in awhile, but.. why? Who uses editors that can't block comment/uncomment anymore? :( -- --S .... p.s: change the ".invalid" to ".com" in email address to reply privately.
From: Raymond Hettinger on 28 Mar 2010 02:36 On Mar 26, 7:49 am, kj <no.em...(a)please.post> wrote: > What's the word on using "classes as namespaces"? E.g. > > class _cfg(object): > spam = 1 > jambon = 3 > huevos = 2 > > breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos) Works for me. Raymond
From: News123 on 28 Mar 2010 06:25 Hi STephen, Stephen Hansen wrote: > That said... > >> [*] My own subjective dislike for the widespread practice of using >> triple quotes to comment out code is formally similar to this one >> ("the 'intended use' for triple-quoting is not to comment out code", >> etc.). Here I find myself on the opposite side of the purist/pragmatic >> divide. Hmmm. > > What?! > > Where do you get this "widespread practice"? You mentioned that before > when you last posted about that and I forgot to comment. I've never seen > it. I wouldn't say it's wide spread, but definitely something one encounters. Especially with python rather new to python > > In the 110k lines of in-house code I maintain, we don't use it once; we > have somewhere around 300k lines of third-party code from a wide range > of sources, and although I haven't reviewed it all by any means, I > regularly have to peek over it and I never seen triple quoted "comments". > > Hell, I almost never see commented -code-. Code should only be commented > while fiddling or debugging. Once fiddlng is done, dead code should be > removed. > > I'm sure it -happens- every once in awhile, but.. why? Who uses editors > that can't block comment/uncomment anymore? :( I had to explain block comment / uncomment to some collegues before the triple quote commenting disappeared from our code. Unfortunaltely everybody uses a different type of editor, so I googled for them to show them what their editors can do. You'd be surprised how many people do neither master their editors nor care for it. bye N
From: Lie Ryan on 30 Mar 2010 07:39 On 03/27/2010 10:28 PM, Jonathan Hartley wrote: > one might like to name the complex block of logic, just to make it > readable: > > > x = 1 > def account_for_non_square_pixels(x): > ((some complex logic)) > account_for_non_square_pixels() > y = 2 > > > But defining and then calling the function like that is a tad > cumbersome. So I was wondering about: > I never liked the narrow definition of function as "reusable piece of code". This narrow definition implies that a piece of code used only once do not need to be made a function. I would rather define function as "a logically independent piece of code" and encourage refactorizing code into functions even if they are only used once as long as they are conceptually a "step" and being able to reuse code as a nice side-effect of it. Under this definition, the "some complex logic" conceptually is an independent piece of code that can (and probably should) be factorized.
From: Aahz on 10 Apr 2010 13:53
In article <hoihgt$p6t$1(a)reader1.panix.com>, kj <no.email(a)please.post> wrote: > >What's the word on using "classes as namespaces"? E.g. > >class _cfg(object): > spam = 1 > jambon = 3 > huevos = 2 > >breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos) There is one gotcha associated with using classes as namespaces: you have to be careful to avoid instantiating them. That goes triple if you modify the class attributes, because modifying an attribute in an instance does *not* propagate the change to the class. -- Aahz (aahz(a)pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan |