Re: DRY and class variables

2011-09-08 Thread egbert
On Thu, Sep 08, 2011 at 12:22:33PM +0200, Thomas Jollans wrote: > You should be able to do this with a metaclass (almost certainly > overkill), or with a class decorator (Python 2.6+): > > def with_metadata (cls): > cls.m = Metadata (cls.__name__) > cls.m.do_something () > return cls >

Re: DRY and class variables

2011-09-08 Thread Eric Snow
On Thu, Sep 8, 2011 at 3:55 AM, egbert wrote: > My classes correspond to sql tables. > In these classes I want to have several class variables > that refer to data that are common to all instances. > > The assignment statements for the class variables are the same > in all class

Re: DRY and class variables

2011-09-08 Thread Thomas Jollans
On 08/09/11 11:55, egbert wrote: > My classes correspond to sql tables. > In these classes I want to have several class variables > that refer to data that are common to all instances. > > The assignment statements for the class variables are the same > in all classes, ex

DRY and class variables

2011-09-08 Thread egbert
My classes correspond to sql tables. In these classes I want to have several class variables that refer to data that are common to all instances. The assignment statements for the class variables are the same in all classes, except that of these instructions needs the name of the class itself

Re: use class factory to set required class variables?

2011-01-27 Thread Bill Felton
On Jan 27, 2011, at 4:03 AM, Steven D'Aprano wrote: > On Wed, 26 Jan 2011 13:37:20 -0800, Alan wrote: > >> I have a class ``A`` that is intentionally incomplete: it has methods >> that refer to class variables that do not exist. > > For the record, in P

Re: use class factory to set required class variables?

2011-01-27 Thread Jean-Michel Pichavant
Alan wrote: I have a class ``A`` that is intentionally incomplete: it has methods that refer to class variables that do not exist. The class ``A`` has several complicated methods, a number of which reference the "missing" class variables. Obviously, I do not directly use ``A``. I ha

Re: use class factory to set required class variables?

2011-01-27 Thread Steven D'Aprano
On Wed, 26 Jan 2011 13:37:20 -0800, Alan wrote: > I have a class ``A`` that is intentionally incomplete: it has methods > that refer to class variables that do not exist. For the record, in Python it is usual to refer to "attributes" rather than "class variables" an

Re: use class factory to set required class variables?

2011-01-26 Thread Alan
On Jan 26, 4:37 pm, Alan wrote: > I have a class factory ``f``` that subclasses ``A`` *only* in > order to define the class variables. I suppose it would be clearer to say that `f` *returns* subclasses of `A`. Hopefully that was clear ... Alan Isaac -- http://mail.python.org/mailman/li

use class factory to set required class variables?

2011-01-26 Thread Alan
I have a class ``A`` that is intentionally incomplete: it has methods that refer to class variables that do not exist. The class ``A`` has several complicated methods, a number of which reference the "missing" class variables. Obviously, I do not directly use ``A``. I have a class f

Re: scope of generators, class variables, resulting in global na

2010-02-28 Thread dontspamleo
ble with the same name as a global variable? Are there other use cases that would break? > > > 2. What are the advantages of making the scope of class variables > > different? Maybe is it just a historical trait? > > See the discussion in the PEP for introducing nested scopes in t

Re: scope of generators, class variables, resulting in global na

2010-02-27 Thread Steven D'Aprano
1. Has this been discussed before? Yes. > 1. What would this suggestion break? Nearly all existing code using classes, which is nearly everything. > 2. What are the advantages of making the scope of class variables > different? Maybe is it just a historical trait? See the discussion in the

Re: scope of generators, class variables, resulting in global na

2010-02-27 Thread sstein...@gmail.com
On Feb 27, 2010, at 6:57 PM, dontspamleo wrote: > > > http://bioscreencastwiki.com/Python_Variable_scope_gymnastics Broken link: Site settings could not be loaded We were unable to locate the API to request site settings. Please see below for debugging information. HTTP Response Status Cod

Re: scope of generators, class variables, resulting in global na

2010-02-27 Thread dontspamleo
e? 1. What would this suggestion break? 2. What are the advantages of making the scope of class variables different? Maybe is it just a historical trait? Cheers, Leo. -- http://mail.python.org/mailman/listinfo/python-list

Re: scope of generators, class variables, resulting in global na

2010-02-25 Thread Arnaud Delobelle
dontspamleo writes: > Hi Arnaud et al, > > Here is the link to the bug report from which the discussion in PEP > 289 was extracted: > > http://bugs.python.org/issue872326 > > It looks like they were fixing a bunch of bugs and that this > discussion was one of the many in that thread. > > Here is

Re: scope of generators, class variables, resulting in global na

2010-02-25 Thread dontspamleo
Hi Arnaud et al, Here is the link to the bug report from which the discussion in PEP 289 was extracted: http://bugs.python.org/issue872326 It looks like they were fixing a bunch of bugs and that this discussion was one of the many in that thread. Here is another post which points to the core of

Re: scope of generators, class variables, resulting in global na

2010-02-24 Thread Arnaud Delobelle
dontspamleo writes: > @Arnaud: I tried to find your earlier post -- googled "Arnaud lambda" > -- but couldn't. I remembered after posting that I sent this to python-ideas. Here is the first message in the thread: http://mail.python.org/pipermail/python-ideas/2007-December/001260.html In this t

Re: scope of generators, class variables, resulting in global na

2010-02-24 Thread dontspamleo
Hi Folks, Thanks everyone for the great contributions! I understand this better now. The distinction between a shorthand for a function definition and a shorthand for a loop iteration is crucial. Also: thanks for pointing out the even the list comprehension doesn't work in py3. That was incredibl

Re: scope of generators, class variables, resulting in global na

2010-02-24 Thread Arnaud Delobelle
Nomen Nescio wrote: > Hello, > > Can someone help me understand what is wrong with this example? > > class T: > A = range(2) > B = range(4) > s = sum(i*j for i in A for j in B) > > It produces the exception: > > : global name 'j' is not defined It's due to scoping rules for classes and/or

Re: scope of generators, class variables, resulting in global na

2010-02-24 Thread Jon Clements
On Feb 24, 12:21 pm, "Alf P. Steinbach" wrote: > * Nomen Nescio: > > > Hello, > > > Can someone help me understand what is wrong with this example? > > > class T: > >   A = range(2) > >   B = range(4) > >   s = sum(i*j for i in A for j in B) > > > It produces the exception: > > > : global name 'j'

Re: scope of generators, class variables, resulting in global na

2010-02-24 Thread b3ng0
On Feb 24, 5:52 am, Nomen Nescio wrote: > Hello, > > Can someone help me understand what is wrong with this example? > > class T: >   A = range(2) >   B = range(4) >   s = sum(i*j for i in A for j in B) > > It produces the exception: > > : global name 'j' is not defined > > The exception above is

Re: scope of generators, class variables, resulting in global na

2010-02-24 Thread Alf P. Steinbach
* Nomen Nescio: Hello, Can someone help me understand what is wrong with this example? class T: A = range(2) B = range(4) s = sum(i*j for i in A for j in B) It produces the exception: : global name 'j' is not defined Which Python implementation are you using? I can't reproduce the er

scope of generators, class variables, resulting in global na

2010-02-24 Thread Nomen Nescio
Hello, Can someone help me understand what is wrong with this example? class T: A = range(2) B = range(4) s = sum(i*j for i in A for j in B) It produces the exception: : global name 'j' is not defined The exception above is especially confusing since the following similar example (I jus

Re: Union of class variables

2010-02-16 Thread Ben Finney
Joan Miller writes: > Is possible to get a third class with the class variables of another > two classes? Multiple inheritance is allowed in Python: class Baz(Foo, Bar): pass However, it leads to complications that you likely don't want, e.g. http://www.artima.

Re: Union of class variables [Solved]

2010-02-16 Thread Joan Miller
On 16 feb, 08:40, alex23 wrote: > On Feb 16, 6:16 pm, Joan Miller wrote: > > > Is possible to get a third class with the class variables of another > > two classes? > > > > > class A: > >     foo = 1 > > > class B: > >    

Re: Union of class variables

2010-02-16 Thread alex23
On Feb 16, 6:16 pm, Joan Miller wrote: > Is possible to get a third class with the class variables of another > two classes? > > > class A: >     foo = 1 > > class B: >     bar = 2 > Through multiple inheritance? >>> class C(A,

Re: Class variables static by default?

2009-12-22 Thread Daniel Fetchinson
do get a mental "double-take" every time I read about "class variables". > It takes a real effort of will to remind myself that they're probably not > talking about something like this: > > for theclass in (int, float, Decimal, Fraction): > do_something

Re: Class variables static by default?

2009-12-21 Thread Steven D'Aprano
a/C++ background) I actually do get a mental "double-take" every time I read about "class variables". It takes a real effort of will to remind myself that they're probably not talking about something like this: for theclass in (int, float, Decimal, Fraction): do_som

Re: Class variables static by default?

2009-12-21 Thread Daniel Fetchinson
able", but you'll have to > rewrite parts of the official Python documentation so it becomes > consistent with it. The phrase "class variable" appears about 30 times, > always meaning "class attribute"; four of them in the Language Reference, > section "

Re: Class variables static by default?

2009-12-20 Thread Gabriel Genellina
.name“, and an instance variable hides a class variable with the same name when accessed in this way. Class variables can be used as defaults for instance variables, but using mutable values there can lead to unexpected results. For new-style classes, descriptors can be used to create instance var

Re: Class variables static by default?

2009-12-20 Thread Steven D'Aprano
On Sat, 19 Dec 2009 20:28:07 -0800, Chris Rebert wrote: >> Surely, since string variables are strings, and float variables are >> floats, and bool variables are bools, and module variables are modules, >> a class variable will be a class and an instance variable will be an >> instance? > > As the

Re: Class variables static by default?

2009-12-19 Thread Chris Rebert
On Sat, Dec 19, 2009 at 8:16 PM, Steven D'Aprano wrote: > On Sun, 20 Dec 2009 11:44:11 +1100, Lie Ryan wrote: > >> In python, 'class variable' is a variable that belongs to a class; not >> to the instance and is shared by all instance that belong to the class. > > Surely, since string variables ar

Re: Class variables static by default?

2009-12-19 Thread Steven D'Aprano
On Sun, 20 Dec 2009 11:44:11 +1100, Lie Ryan wrote: > In python, 'class variable' is a variable that belongs to a class; not > to the instance and is shared by all instance that belong to the class. Surely, since string variables are strings, and float variables are floats, and bool variables ar

Re: Class variables static by default?

2009-12-19 Thread Lie Ryan
On 12/20/2009 11:10 AM, KarlRixon wrote: Given the following script, I'd expect p1.items to just contain ["foo"] and p2.items to contain ["bar"] but they both contain ["foo", "bar"]. Why is this? Are object variables not specific to their instance? First of all, dump all the preconception of w

Re: Class variables static by default?

2009-12-19 Thread Cameron Simpson
On 19Dec2009 16:10, KarlRixon wrote: | Given the following script, I'd expect p1.items to just contain | ["foo"] and p2.items to contain ["bar"] but they both contain ["foo", | "bar"]. | | Why is this? Are object variables not specific to their instance? You haven't instatiated "items" in the ob

Re: Class variables static by default?

2009-12-19 Thread Xavier Ho
Yes, if you want instance variables that are unique to each instance of a class, do the following: class Parser: def __init__(self): self.items = [] And that should work fine. J:\_Programming Projects\Python>python test.py <__main__.Parser object at 0x0240E7B0> <__main__.Parser object

Re: Class variables static by default?

2009-12-19 Thread John Posner
On Sat, 19 Dec 2009 19:10:13 -0500, KarlRixon wrote: Given the following script, I'd expect p1.items to just contain ["foo"] and p2.items to contain ["bar"] but they both contain ["foo", "bar"]. Why is this? Are object variables not specific to their instance? --- #!/u

Class variables static by default?

2009-12-19 Thread KarlRixon
Given the following script, I'd expect p1.items to just contain ["foo"] and p2.items to contain ["bar"] but they both contain ["foo", "bar"]. Why is this? Are object variables not specific to their instance? --- #!/usr/bin/env python class Parser: items = []

Re: class variables and class methods

2009-04-18 Thread Aahz
In article <9c848013-2245-455e-bb30-48e430d56...@j9g2000prh.googlegroups.com>, wrote: > >I have a class whose job is to serve several other objects, [...] Sorry, I'm finding it difficult to understand what you want. It looks to me that you're confusing "object" and "instance", and I think you'

class variables and class methods

2009-04-17 Thread krishnapostings
also no need to have object methods, all could be class methods (my understanding of benefits between class and objects is not good, see below). 1. Should I have one object 'O1' that serves all i.e., no class variables, class methods, if so, since this class is in a module ('M1&#

Re: Global dictionary or class variables

2008-10-26 Thread Arnaud Delobelle
x27;D': 2, 'E': 4 } > > This actually works fine. I was just thinking if it wasn't better to > use class variables. > > Since I have a class Note, I could write: > > class Note: >     C = 0 >     D = 2 >     ... > > Which style maybe better?

Re: Global dictionary or class variables

2008-10-25 Thread Fuzzyman
x27;D': 2, 'E': 4 } > > This actually works fine. I was just thinking if it wasn't better to > use class variables. > > Since I have a class Note, I could write: > > class Note: >     C = 0 >     D = 2 >     ... > > Which style maybe bette

Re: Global dictionary or class variables

2008-10-24 Thread Matimus
x27;D': 2, 'E': 4 } > > This actually works fine. I was just thinking if it wasn't better to > use class variables. > > Since I have a class Note, I could write: > > class Note: >     C = 0 >     D = 2 >     ... > > Which style maybe better?

Re: Global dictionary or class variables

2008-10-24 Thread Chris Rebert
;C': 0, 'D': 2, 'E': 4 } > > This actually works fine. I was just thinking if it wasn't better to > use class variables. > > Since I have a class Note, I could write: > > class Note: >C = 0 >D = 2 >... > > Which style m

Global dictionary or class variables

2008-10-24 Thread Mr . SpOOn
Hi, in an application I have to use some variables with fixed valuse. For example, I'm working with musical notes, so I have a global dictionary like this: natural_notes = {'C': 0, 'D': 2, 'E': 4 } This actually works fine. I was just thinking if it w

Re: Behavior of mutable class variables

2007-05-09 Thread castironpi
I immediately see its utility. Thanks again > > class Stock(object): > NStocks = [] #Class variables > N1 = 0 > > @classmethod > def cleanUp(cls): > Stocks.NStocks = [] > Stocks.N1 = 0 > > def simulation(N, par1,

Re: Behavior of mutable class variables

2007-05-09 Thread tkpmep
ock(object): NStocks = [] #Class variables N1 = 0 @classmethod def cleanUp(cls): Stocks.NStocks = [] Stocks.N1 = 0 def simulation(N, par1, par2, idList, returnHistoryDir): Stock.cleanUp() results = .. print results. -- http://mail.

Re: Behavior of mutable class variables

2007-05-09 Thread tkpmep
On May 9, 5:25 pm, [EMAIL PROTECTED] wrote: > To test some theories, I created a new class variable, an int named Diez, Thanks. It is for precisely this reason that I added another class variable - the immutable int N1. But this too keeps getting incremented on subsequent calls to simulation( ). I

Re: Behavior of mutable class variables

2007-05-09 Thread Terry Reedy
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | Here's what I expect to happen each time simulation( ) is called: the | class variable NStocks for the class Stock is initialized to an empty | list, Why would you expect that ;-) A class statement is usually executed exactly once, as

Re: Behavior of mutable class variables

2007-05-09 Thread tkpmep
To test some theories, I created a new class variable, an int named N1, which is not mutable. So my Stock class now looks as follows: class Stock(object): NStocks = [] #Class variables N1 = 0 def __init__(self, id, returnHistory): self.id = id

Re: Behavior of mutable class variables

2007-05-09 Thread Diez B. Roggisch
[EMAIL PROTECTED] schrieb: > I have written a program that runs portfolio simulations with > different parameters and prints the output, but am mystified by the > behavior of a mutable class variable. A simplified version of the > program follows - would you kindly help me understand why it behaves

Behavior of mutable class variables

2007-05-09 Thread tkpmep
I have written a program that runs portfolio simulations with different parameters and prints the output, but am mystified by the behavior of a mutable class variable. A simplified version of the program follows - would you kindly help me understand why it behaves the way it does. The function mai

Re: Problem with class variables

2007-03-29 Thread Thomas Krüger
Dag schrieb: > I have a problem which is probaly very simple to solve, but I can't > find a nice solution. > I have the following code > > class Test: > def __init__(self): > self.a=[1,2,3,4] > self.b=self.a > def swap(self): > self.a[0],self.a[3]=self.a[3],self.a[

Re: Problem with class variables

2007-03-29 Thread kyosohma
code > > > class Test: > > def __init__(self): > > self.a=[1,2,3,4] > > self.b=self.a > > self.b = list(self.a) > > BTW this is about instance variable, not class variables. > > Ciao, > Marc 'BlackJack' Rintsch One

Re: Problem with class variables

2007-03-29 Thread Marc 'BlackJack' Rintsch
self.b = list(self.a) BTW this is about instance variable, not class variables. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list

Problem with class variables

2007-03-29 Thread Dag
I have a problem which is probaly very simple to solve, but I can't find a nice solution. I have the following code class Test: def __init__(self): self.a=[1,2,3,4] self.b=self.a def swap(self): self.a[0],self.a[3]=self.a[3],self.a[0] def prnt(self): pr

Re: Local class variables? (mod_python problem)

2007-02-23 Thread Diez B. Roggisch
> > Many thanks for your reply. The use case is per request, and I would be > grateful to learn more about thread-local storage. There are several ways. I'm not familiar with mod_python, but I guess you get a dict-like object for the request parameters. In python, this is usually writable (in con

Re: Local class variables? (mod_python problem)

2007-02-23 Thread Rory Campbell-Lange
On 23/02/07, Diez B. Roggisch ([EMAIL PROTECTED]) wrote: > > It is not desirable for the class variable to keep incrementing outside > > of invocations of '__main__', as is the case when it is loaded under > > mod_python under apache2 on linux. > > > > I'm still not clear on what you want to acco

Re: Local class variables? (mod_python problem)

2007-02-23 Thread Diez B. Roggisch
> It is not desirable for the class variable to keep incrementing outside > of invocations of '__main__', as is the case when it is loaded under > mod_python under apache2 on linux. > I'm still not clear on what you want to accomplish. In the end it boils down to who is supposed to share that inf

Re: Local class variables? (mod_python problem)

2007-02-22 Thread greg
be gratefully received. > > Kind regards > Rory > > > On 22/02/07, Rory Campbell-Lange ([EMAIL PROTECTED]) wrote: > >>We have a set of classes using static methods to retain reference >>variables between operations. The problem is that the static variables >>

Re: Local class variables? (mod_python problem)

2007-02-22 Thread Rory Campbell-Lange
On 22/02/07, Rory Campbell-Lange ([EMAIL PROTECTED]) wrote: > In essence we use class variables as follows: > > class Part (object): > totalgia = 0 > def __init__(self, gia): > self.gia = gia # gross internal area >

Re: Local class variables? (mod_python problem)

2007-02-22 Thread Piet van Oostrum
> Rory Campbell-Lange <[EMAIL PROTECTED]> (RC) wrote: >RC> totalgia keeps incrementing when this code is used under mod_python. And also when used outside of mod_python. It is because it is a class level variable. In fact I think under certain circumstances in mod_python it will not do that b

Re: Local class variables? (mod_python problem)

2007-02-22 Thread Rory Campbell-Lange
Apologies to Piet and Diez for the lack of clarity in my previous post (and the broken code). In essence we use class variables as follows: class Part (object): totalgia = 0 def __init__(self, gia): self.gia = gia # gross internal area self.giaratio

Re: Local class variables? (mod_python problem)

2007-02-22 Thread Diez B. Roggisch
Rory Campbell-Lange wrote: > We have a set of classes using static methods to retain reference > variables between operations. The problem is that the static variables > are not reset between operations when used through mod_python. > > Although it is possible to reset the

Re: Local class variables? (mod_python problem)

2007-02-22 Thread Piet van Oostrum
hrough mod_python. >RC> Although it is possible to reset the class variables between invocations >RC> of the system, this has the potential of 'wiping out' these variables >RC> when another user is using the system. >RC> Is there a way of getting the equivalent of &#

Local class variables? (mod_python problem)

2007-02-22 Thread Rory Campbell-Lange
We have a set of classes using static methods to retain reference variables between operations. The problem is that the static variables are not reset between operations when used through mod_python. Although it is possible to reset the class variables between invocations of the system, this has

Re: Accessing class variables in staticmethods.

2007-01-21 Thread Ramashish Baranwal
Sam wrote: > On 21 Jan 2007 12:49:17 -0800, Ramashish Baranwal > <[EMAIL PROTECTED]> wrote: > > class Base: > > staticvar = 'Base' > > > > @staticmethod > > def printname(): > > # this doesn't work > > # print staticvar > > # this does work but derived classes wo

Re: Accessing class variables in staticmethods.

2007-01-21 Thread Sam
On 21 Jan 2007 12:49:17 -0800, Ramashish Baranwal <[EMAIL PROTECTED]> wrote: > class Base: > staticvar = 'Base' > > @staticmethod > def printname(): > # this doesn't work > # print staticvar > # this does work but derived classes wouldn't behave as I want >

Accessing class variables in staticmethods.

2007-01-21 Thread Ramashish Baranwal
Hi, I want to access a static variable in a staticmethod. The variable can be redefined by derived classes and that should be reflected in base's staticmethod. Consider this trivial example- class Base: staticvar = 'Base' @staticmethod def printname(): # this doesn't work

Re: class variables

2006-08-08 Thread Andre Meyer
On 7/31/06, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Colin J. Williams wrote: > > Andre Meyer wrote: > >> Hi all > >> > >> I am trying to understand the magic of Python's class variables and > >> tried the following code (see belo

Re: class variables

2006-07-31 Thread Bruno Desthuilliers
Colin J. Williams wrote: > Andre Meyer wrote: >> Hi all >> >> I am trying to understand the magic of Python's class variables and >> tried the following code (see below). >> >> Just out of curiosity, I tried to define a property that provides >> ac

Re: class variables

2006-07-30 Thread faulkner
python != java. when you say "self.v = ...", you mask the class attribute with an instance attribute. say "C1.v = ...". Colin J. Williams wrote: > Andre Meyer wrote: > > Hi all > > > > I am trying to understand the magic of Python's class variabl

Re: class variables

2006-07-30 Thread Colin J. Williams
Andre Meyer wrote: > Hi all > > I am trying to understand the magic of Python's class variables and > tried the following code (see below). > > Just out of curiosity, I tried to define a property that provides access > to a seemingly instancae variable which is in

class variables

2006-07-27 Thread Andre Meyer
Hi allI am trying to understand the magic of Python's class variables and tried the following code (see below).Just out of curiosity, I tried to define a property that provides access to a seemingly instancae variable which is in fact a class variable. All seems to work fine (case 4), but w

class variables

2006-07-27 Thread Andre Meyer
Hi allI am trying to understand the magic of Python's class variables and tried the following code (see below).Just out of curiosity, I tried to define a property that provides access to a seemingly instancae variable which is in fact a class variable. All seems to work fine (case 4), but w

Re: Using metaclasses to inherit class variables

2006-05-22 Thread Steven Bethard
[EMAIL PROTECTED] wrote: > Fresh copies of class vars so the first one is the correct: ('foo', > 'bar', [], False) Ahh, yeah, then you definitely need the copy.copy call. import copy class ClassVars(type): > ... def __init__(cls, name, bases, dict): > ... for name, valu

Re: Using metaclasses to inherit class variables

2006-05-22 Thread [EMAIL PROTECTED]
Sorry for not being clear. Fresh copies of class vars so the first one is the correct: ('foo', 'bar', [], False) >>> import copy >>> >>> class ClassVars(type): ... def __init__(cls, name, bases, dict): ... for name, value in type(cls).classVars.iteritems(): ... if name not

Re: Using metaclasses to inherit class variables

2006-05-22 Thread Steven Bethard
[EMAIL PROTECTED] wrote: > Oops! This isn't working. As the sequence I'm trying for is def set_classvars(**kwargs): > ... def __metaclass__(name, bases, classdict): > ... for name, value in kwargs.iteritems(): > ... if name not in classdict: > ...

Re: Using metaclasses to inherit class variables

2006-05-22 Thread [EMAIL PROTECTED]
Oops! This isn't working. As the sequence I'm trying for is >>> def set_classvars(**kwargs): ... def __metaclass__(name, bases, classdict): ... for name, value in kwargs.iteritems(): ... if name not in classdict: ... classdict[name] = value ...

Re: Using metaclasses to inherit class variables

2006-05-22 Thread [EMAIL PROTECTED]
Much better. Thanks again. -- http://mail.python.org/mailman/listinfo/python-list

Re: Using metaclasses to inherit class variables

2006-05-22 Thread Steven Bethard
[EMAIL PROTECTED] wrote: > OK no question. I'm only posting b/c it may be something another newbie > will want to google in the future. Now that I've worked thru the > process this turns out to be fairly easy. > > However, if there are better ways please let me know. > > Module = ClassVars.py >

Re: Using metaclasses to inherit class variables

2006-05-22 Thread [EMAIL PROTECTED]
OK no question. I'm only posting b/c it may be something another newbie will want to google in the future. Now that I've worked thru the process this turns out to be fairly easy. However, if there are better ways please let me know. Module = ClassVars.py import copy class ClassVars(type): c

Re: Using metaclasses to inherit class variables

2006-05-21 Thread [EMAIL PROTECTED]
situation were I wanted to use fresh copies of class variables to define the abstracted class. Given that I'd onlly save (2*3) 6 lines of code in my application, I figured that move on for now. -- http://mail.python.org/mailman/listinfo/python-list

Re: Using metaclasses to inherit class variables

2006-05-20 Thread [EMAIL PROTECTED]
Hmm. setattr() only does a shallow search. Good to know. Your if not name in dict: setattr(cls, name, value) is a more succinct/better way of writing if not cls.__dict__.has_key(var): setattr(cls, var, val) Which i tested a fair bit. OK it appears that both are working for the simple

Re: Using metaclasses to inherit class variables

2006-05-19 Thread Steven Bethard
[EMAIL PROTECTED] wrote: > I want to inherit fresh copies of some class variables. So I set up a > metaclass and meddle with the class variables there. > > Now it would be convenient to run thru a dictionary rather than > explicitly set each variable. However getattr() and se

Using metaclasses to inherit class variables

2006-05-19 Thread [EMAIL PROTECTED]
I want to inherit fresh copies of some class variables. So I set up a metaclass and meddle with the class variables there. Now it would be convenient to run thru a dictionary rather than explicitly set each variable. However getattr() and setattr() are out because they chase the variable thru the

Re: Is it better to use class variables or pass parameters?

2006-03-16 Thread Terry Hancock
evaluate" method. Now you have > to repeatedly pass the peptide and peptide_name to each > function. According to what everyone has said declaring > them as class variables is bad because they are not > related to the state of the "PeptideEvaluator". How can I > avoid havin

Re: Is it better to use class variables or pass parameters?

2006-03-16 Thread Steve Holden
e a different colour. [This may be what you mean in whcih case please regard this as correcting your language, not your thinking. Class variables are available, and are normally single values share among all instances of a class.]. > However, if the wallet class contains a function to compute

Re: Is it better to use class variables or pass parameters?

2006-03-16 Thread Eric Brunel
f.write(peptide) > f.close() > > def test3(self, peptide, peptide_name): > f = open(peptide_name + ".txt", "w") > f.write(peptide) > f.close() > > So, you instantiate a class called "PeptideEvaluator" and pass in ea

Re: Is it better to use class variables or pass parameters?

2006-03-15 Thread Terry Reedy
"Derek Basch" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > So, if I am understanding what everyone is saying here. I should do my > best to distinguish between values that are part of the "state" of an > object and values that are more disposable and can change for each > computa

Re: Is it better to use class variables or pass parameters?

2006-03-15 Thread Derek Basch
f.write(peptide) f.close() So, you instantiate a class called "PeptideEvaluator" and pass in each string to its "evaluate" method. Now you have to repeatedly pass the peptide and peptide_name to each function. According to what everyone has said declaring them as clas

Re: Is it better to use class variables or pass parameters?

2006-03-15 Thread Derek Basch
So, if I am understanding what everyone is saying here. I should do my best to distinguish between values that are part of the "state" of an object and values that are more disposable and can change for each computation of a value. So if I create an instance of a "wallet" class and the color of the

Re: Is it better to use class variables or pass parameters?

2006-03-13 Thread Derek Basch
Wow! Thanks everyone. Such coherent and philisophical answers. I will read them all over on a lazy sunday as this type ethereal stuff hurts my head after about 30 seconds. All the gurus on the python list are so friggin' smart. This should improve my coding after I digest it all. Thanks Again! Der

Re: class variables for subclasses tuple

2006-03-08 Thread Peter Otten
[EMAIL PROTECTED] wrote: > In passing, I have another question: where can I read up more on > metaclasses? Well, in "Python in a Nutshell" Alex Martelli manages to pack the practical information that lets you work with metaclasses into just four pages, including a two-page example. You may have s

Re: class variables for subclasses tuple

2006-03-08 Thread alainpoint
Thank you Peter, this does the job. In passing, I have another question: where can I read up more on metaclasses? Alain -- http://mail.python.org/mailman/listinfo/python-list

Re: class variables for subclasses tuple

2006-03-08 Thread Peter Otten
[EMAIL PROTECTED] wrote: > > Peter Otten wrote: >> [EMAIL PROTECTED] wrote: >> >> > Point.x=0 leads to having p.x==0 >> > It seems not possible to have class variables and instance variable >> > having the same name and yet different values.

Re: class variables for subclasses tuple

2006-03-08 Thread alainpoint
As an supplement to my previous post, please find hereunder a snippet for my unsuccessful attempt (commented out snippet does not work): def superTuple(*attribute_names): nargs = len(attribute_names) class T(tuple): def __new__(cls, *args): re

Re: class variables for subclasses tuple

2006-03-08 Thread alainpoint
Peter Otten wrote: > [EMAIL PROTECTED] wrote: > > > Point.x=0 leads to having p.x==0 > > It seems not possible to have class variables and instance variable > > having the same name and yet different values. > > A quick check: > > >>> class

Re: class variables for subclasses tuple

2006-03-08 Thread Peter Otten
[EMAIL PROTECTED] wrote: > Point.x=0 leads to having p.x==0 > It seems not possible to have class variables and instance variable > having the same name and yet different values. A quick check: >>> class T(tuple): ... class __metaclass__(type): ... x = prop

class variables for subclasses tuple

2006-03-08 Thread alainpoint
pr, self))) # add a few key touches to our new subclass of `tuple' for index, attr_name in enumerate(attribute_names): setattr(supertup, attr_name, property(itemgetter(index))) supertup._ _name_ _ = typename return supertup Now my problem is: i would like to extend this

Re: Is it better to use class variables or pass parameters?

2006-03-02 Thread bruno at modulix
Derek Basch wrote: > This one has always bugged me. Is it better to just slap a "self" in > front of any variable that will be used by more than one class method s/class method/method/ In Python, a "class method" is a method working on the class itself (not on a given instance). > or should I pa

  1   2   >