handling many default values
Gabriel Genellina
gagsl-py at yahoo.com.ar
Fri Nov 10 18:36:27 EST 2006
At Friday 10/11/2006 14:11, Alan G Isaac wrote:
>My class MyClass reuses many default parameters
>with a small number of changes in each instance.
>For various reasons I decided to put all the
>parameters in a separate Params class, instances
>of which reset the default values based on keyword
>arguments, like this:
>
>class Params:
> def __init__(self,**kwargs):
> #set lots of default values
> ...
> #set the deviations from defaults
> self.__dict__.update(kwargs)
>
>Is this a reasonable approach overall?
>(Including the last line.)
I'm not sure what you want to do exactly, but a class attribute acts
as a default instance attribute.
class A(object):
x = 0
y = 20
def __init__(self, x=None, y=None):
if x is not None: self.x = x
if y is not None: self.y = y
class B(A):
z = 3
def __init__(self, x=None, y=None, z=None):
A.__init__(self, x, y)
if z is not None: self.z = z
a = A(1)
print "a.x=",a.x
print "a.y=",a.y
b = B(z=8)
print "b.x=",b.x
print "b.y=",b.y
print "b.z=",b.z
output:
a.x= 1
a.y= 20
b.x= 0
b.y= 20
b.z= 8
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
More information about the Python-list
mailing list