处理许多默认值 [英] handling many default values

查看:54
本文介绍了处理许多默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级MyClass重复使用许多默认参数

,每个实例都有少量更改。

由于各种原因我决定把所有的

参数在一个单独的Params类中,实例

根据关键字

参数重置默认值,如下所示:


class Params:

def __init __(self,** kwargs):

#set许多默认值

...

#设置与默认值的偏差

self .__ dict __。update(kwargs)


这是一个合理的方法吗?

(包括最后一行。)


谢谢,

Alan Isaac

解决方案

在星期五10/11/2006 14:11,Alan G Isaac写道:


>我的类MyClass重用许多默认参数<每个实例中都有少量更改。
由于各种原因,我决定放入所有的
参数在一个单独的Params类中,实例
根据关键字
参数重置默认值,如下所示:

class Params:

def __init __(self,** kwargs):

#set许多默认值

...

#设置默认值的偏差

self .__ dict __。update(kwargs)

这是一个合理的方法吗?
(包括最后一行。)



我不确定你想要做什么,但是一个class属性作为默认实例属性扮演




A级(对象):

x = 0

y = 20

def __init __(self,x = None,y = None):

如果x不是None:self.x = x

如果y不是None:self.y = y


class B (A):

z = 3

def __init __(自我,x =无,y =无,z =无):

A. __init __(self,x,y)

如果z不是None:self.z = z


a = A(1)

print" ax =" ;, ax

print" ay =" ;, ay


b = B(z = 8 )

print" bx =" ;,bx

print" by =",

print" bz =" ,bz


输出:


ax = 1

ay = 20

bx = 0

by = 20

bz = 8

-

Gabriel Genellina

Softlab SRL


__________________________________________________

Correo Yahoo!

Espacio para todos tus mensajes,antivirus y antispam?gratis!

?Abrítucuenta ya! - http://correo.yahoo.com.ar


在星期五10/11/2006 14:11,Alan G Isaac写道:


class参数:

def __init __(自我,** kwargs):

#设置多个默认值

...

#设置默认值的偏差

self .__ dict __。update(kwargs)


这是一个合理的方法吗?

(包括最后一行。)



" Gabriel Genellina"在留言中写道

新闻:ma ************************************ *** @ pyt hon.org ...


我不确定你想要做什么,但是一个类属性行为

作为默认实例属性。



是的。对不起,我的问题不是很清楚。

有*很多*参数,

,列表可以更改,

所以我想要避免将它们全部列在Param类的__init__函数中,

使用上面的策略。


Q1:这种方法是否合理?

(这是一个关于不可预见的危险的新手问题。)

Q2:在一个单独的类中隔离参数是不是很糟糕?

(评论:目前几个班级可能依赖于(部分)相同的

参数集。)


谢谢,

Alan Isaac


2006年11月10日星期五17:11:24 +0000,Alan G Isaac写道:


My class MyClass重复使用许多默认参数

,每个实例中只有少量更改。



让我看看我明白了。你在做这样的事情吗?


#Class需要很多论据

a = MyClass(0,1,2,3,4,5 ,. ..,99)

#和实例的变化只有一两个args

b = MyClass(0,2,2,3,4,5,.. 。,99)

c = MyClass(0,3,2,3,4,5,...,99)

d = MyClass(0,4,2, 3,4,5,...,99)

e = MyClass(0,5,2,3,4,5,...,99)

。 ...

z = MyClass(0,26,2,3,4,5,...,99)


如果那是'我会认真地重新思考你的班级设计。很难在没有更多关于课程的情况下建议更好的设计,但是如果你不能使用子类来减少数量,我会感到惊讶参数

需要。例如:


class MyClass(object):

def __init __(self,arg0):

self.arg0 = arg0

#现在用合理的值填充参数1到99

class MyClass2(MyClass):

def __init __(self,arg1 ):

""""就像MyClass,但是arg0有一个固定值而arg1有所不同""

super(MyClass,self).__ init__ (固定值)

self.arg1 = arg1


等等你需要的子类。

以同样的方式,也许你可以对这些参数进行分组。例如。这个:

类WordProcessingDoc(对象):

def __init __(self,page_width,page_height,left_margin,

right_margin,top_margin,bottom_margin,font_face,

font_size,font_style,line_size,justification):#以及更多

#和许多,更多参数

pass

创建一些类,并将它们的实例传递给主类:


class CharStyle(object):

def __init __(self,font_face,font_size,font_style):

pass

class PageStyle(object):

def __init __(自我,宽度,高度,左,右,顶部,底部):

通过


类WordProcessingDoc(对象):

def __init __(self,pagestyle,charstyle,paragraphstyle):

pass


由于各种原因我决定把所有的

参数在单独的Params类中,实例

重置默认值基于关键字

参数,如下:


class Params:

def __init __(self,** kwargs):

#set许多默认值

...

#设置默认值的偏差

self .__ dict__。更新(kwargs)


这是一个合理的方法吗?

(包括最后一行。)



(1)如果真的没有替代一个有很多参数的类;

(2)和实例可以不可预测地改变这些参数;

$ b $那么这种方法对我来说似乎是合理的。但我真的建议你

重新考虑你的班级设计。


-

史蒂文。


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.)

Thanks,
Alan Isaac

解决方案

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


At Friday 10/11/2006 14:11, Alan G Isaac wrote:

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.)


"Gabriel Genellina" wrote in message
news:ma***************************************@pyt hon.org...

I''m not sure what you want to do exactly, but a class attribute acts
as a default instance attribute.

Yes. I am sorry my question was not clearer.
There are *many* parameters,
and the list can change,
so I want to avoid listing them all in the Param class''s __init__ function,
using the strategy above.

Q1: Is this approach reasonable?
(This is a newbie question about unforseen hazards.)
Q2: Is it horrible design to isolate the parameters in a separate class?
(Comment: currently several classes may rely on (parts of) the same
parameter set.)

Thanks,
Alan Isaac


On Fri, 10 Nov 2006 17:11:24 +0000, Alan G Isaac wrote:

My class MyClass reuses many default parameters
with a small number of changes in each instance.

Let me see that I understand. Are you doing something like this?

# Class takes a lot of arguments
a = MyClass(0, 1, 2, 3, 4, 5, ..., 99)
# and instances vary only by one or two of those args
b = MyClass(0, 2, 2, 3, 4, 5, ..., 99)
c = MyClass(0, 3, 2, 3, 4, 5, ..., 99)
d = MyClass(0, 4, 2, 3, 4, 5, ..., 99)
e = MyClass(0, 5, 2, 3, 4, 5, ..., 99)
....
z = MyClass(0, 26, 2, 3, 4, 5, ..., 99)

If that''s the case, I''d seriously rethink your class design. It is hard to
advise a better design without knowing more about the class, but I''d be
surprised if you can''t use subclassing to reduce the number of parameters
needed. E.g.:

class MyClass(object):
def __init__(self, arg0):
self.arg0 = arg0
# now fill in arguments 1 through 99 with sensible values

class MyClass2(MyClass):
def __init__(self, arg1):
"""Just like MyClass, but arg0 has a fixed value and arg1 varies"""
super(MyClass, self).__init__("fixed value")
self.arg1 = arg1

and so on for as many subclasses that you need.

In the same way, perhaps you can group those arguments. E.g. instead of
this:
class WordProcessingDoc(object):
def __init__(self, page_width, page_height, left_margin,
right_margin, top_margin, bottom_margin, font_face,
font_size, font_style, line_size, justification): # and many more
# and many, many more arguments
pass
Create some classes, and pass instances of them to the main class:

class CharStyle(object):
def __init__(self, font_face, font_size, font_style):
pass
class PageStyle(object):
def __init__(self, width, height, left, right, top, bottom):
pass

class WordProcessingDoc(object):
def __init__(self, pagestyle, charstyle, paragraphstyle):
pass

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.)

(1) If there really is no alternative to a class with many arguments;
(2) and instances can vary those arguments unpredictably;

then this approach seems reasonable to me. But I really suggest you
rethink your class design.

--
Steven.


这篇关于处理许多默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆