"一旦" Python中的分配 [英] "once" assigment in Python

查看:53
本文介绍了"一旦" Python中的分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我一直在使用Python进行一些DES模拟,因为我们不需要

全速C和它'''写模型的速度要快得多。在

编码期间,我发现分配变量*非常方便,除非已经分配了* b $ b *:我发现这通常被称为一次 ;

分配。


我能用Python得到的最好的是:


尝试:

变量
除了NameError之外的


变量=方法()


我想知道sombody是否有解决方案(技巧,无论......)看起来更加紧凑的编码。类似的东西:


一次(变量,方法)


不起作用,但它会很完美。当然我可以预处理

Python代码,但是全Python解决方案会更方便。


有什么建议吗?


Thx提前,

Lorenzo

解决方案

在消息< 11 *** ******************@w3g2000hsg.googlegroups.c om> ;, Lorenzo Di

Gregorio写道:


在编码过程中,我发现分配变量*非常方便,除非已经分配了* b $ b *:我发现这通常被称为一次

分配。



为什么不在开始时分配一次并完成它?


Lorenzo Di Gregorio写道:


您好,


我一直在使用Python进行DES模拟,因为我们不需要

完整的C速度,编写模型的速度要快得多。在

编码期间,我发现分配变量*非常方便,除非已经分配了* b $ b *:我发现这通常被称为一次 ;

分配。


我能用Python得到的最好的是:


尝试:

变量
除了NameError之外的


变量=方法()


我想知道sombody是否有解决方案(技巧,无论......)看起来更加紧凑的编码。类似的东西:


一次(变量,方法)


不起作用,但它会很完美。当然我可以预处理

Python代码,但是全Python解决方案会更方便。


有什么建议吗?



如果没有愚蠢的话,我建议你重新考虑一下

问题的方法。


你的变量是否有默认值?如果没有,如果用户

没有设置它们并且你的代码试图引用它们会发生什么?如果用户没有设置值,那么你似乎是在试图应用默认值,当

正确的事情是在用户之前应用默认值*得到一个

机会做任何事情*。然后用户做出的任何更改都将覆盖默认值。


更好的是,如果它实用,就是提供你的功能一个

或更多函数,其定义可以设置关键字

参数的默认值。


问候

Steve

-

Steve Holden +1 571 484 6266 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://del.icio.us/steve.holden


对不起,狗吃了我的.sigline


Lorenzo Di Gregorio写道:


我'我一直在使用Python进行一些DES模拟,因为我们不需要完全的C速度,而且编写模型的速度要快得多。在

编码期间,我发现分配变量*非常方便,除非已经分配了* b $ b *:我发现这通常被称为一次 ;

分配。


我能用Python得到的最好的是:


尝试:

变量
除了NameError之外的


变量=方法()


我想知道sombody是否有解决方案(技巧,无论......)看起来更加紧凑的编码。类似的东西:


一次(变量,方法)


不起作用,但它会很完美。当然我可以预处理

Python代码,但是全Python解决方案会更方便。


有什么建议吗?



您可以使用属性来实现延迟评估。或者您可以依赖

命名约定:


> ;> class Once(object):



.... def __getattr __(self,name):

....如果name.startswith(" _calc_"):

....引发AttributeError(" No method to calculation attribute%r"%name [6:])

.... value = getattr(self," _calc_" + name)()

.... setattr(self,name,value)

....返回值

....


>> A类(一次):



.... def _calc_foo(self):

....打印计算foo

....返回42

....


>> a = A()
a.foo



计算foo

42


>> a.foo



42


>> a.bar



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,< module>

文件"< stdin>",第5行,在__getattr__

文件"< stdin>",第4行,在__getattr__

AttributeError:没有方法来计算属性''bar''

< blockquote class =post_quotes>
>> a._calc_bar = lambda:" bar-value"
a.bar



''bar-value''


Peter


Hello,

I''ve been using Python for some DES simulations because we don''t need
full C speed and it''s so much faster for writing models. During
coding I find it handy to assign a variable *unless it has been
already assigned*: I''ve found that this is often referred to as "once"
assigment.

The best I could come up with in Python is:

try:
variable
except NameError:
variable = method()

I wonder if sombody has a solution (trick, whatever ...) which looks
more compact in coding. Something like:

once(variable, method)

doesn''t work, but it would be perfect. Of course I can preprocess the
Python code but an all-Python solution would be more handy.

Any suggestions?

Thx in advance,
Lorenzo

解决方案

In message <11*********************@w3g2000hsg.googlegroups.c om>, Lorenzo Di
Gregorio wrote:

During coding I find it handy to assign a variable *unless it has been
already assigned*: I''ve found that this is often referred to as "once"
assigment.

Why not just assign to it once at the beginning and be done with it?


Lorenzo Di Gregorio wrote:

Hello,

I''ve been using Python for some DES simulations because we don''t need
full C speed and it''s so much faster for writing models. During
coding I find it handy to assign a variable *unless it has been
already assigned*: I''ve found that this is often referred to as "once"
assigment.

The best I could come up with in Python is:

try:
variable
except NameError:
variable = method()

I wonder if sombody has a solution (trick, whatever ...) which looks
more compact in coding. Something like:

once(variable, method)

doesn''t work, but it would be perfect. Of course I can preprocess the
Python code but an all-Python solution would be more handy.

Any suggestions?

Without being fatuous, I would suggest you rethink your approach to the
problem.

Do your variables have default values? If not, what happens if the user
does not set them and your code tries to refer to them? You seem to be
trying to apply defaults if the user hasn''t set a value, when the
correct things to do is to apply the defaults *before the user gets a
chance to do anything at all*. Then any changes made by the user will
overwrite the defaults.

Even better, if its practical, is to provide your functionality as one
or more functions, whose definitions can set default values for keyword
arguments.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline


Lorenzo Di Gregorio wrote:

I''ve been using Python for some DES simulations because we don''t need
full C speed and it''s so much faster for writing models. During
coding I find it handy to assign a variable *unless it has been
already assigned*: I''ve found that this is often referred to as "once"
assigment.

The best I could come up with in Python is:

try:
variable
except NameError:
variable = method()

I wonder if sombody has a solution (trick, whatever ...) which looks
more compact in coding. Something like:

once(variable, method)

doesn''t work, but it would be perfect. Of course I can preprocess the
Python code but an all-Python solution would be more handy.

Any suggestions?

You can use properties to implement lazy evaluation. Or you can rely on a
naming convention:

>>class Once(object):

.... def __getattr__(self, name):
.... if name.startswith("_calc_"):
.... raise AttributeError("No method to calculate attribute %r" % name[6:])
.... value = getattr(self, "_calc_" + name)()
.... setattr(self, name, value)
.... return value
....

>>class A(Once):

.... def _calc_foo(self):
.... print "calculating foo"
.... return 42
....

>>a = A()
a.foo

calculating foo
42

>>a.foo

42

>>a.bar

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in __getattr__
File "<stdin>", line 4, in __getattr__
AttributeError: No method to calculate attribute ''bar''

>>a._calc_bar = lambda: "bar-value"
a.bar

''bar-value''

Peter


这篇关于&QUOT;一旦&QUOT; Python中的分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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