本文介绍了&QUOT;一旦&QUOT; Python中的分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好, 我一直在使用Python进行一些DES模拟,因为我们不需要 全速C和它'''写模型的速度要快得多。在 编码期间,我发现分配变量*非常方便,除非已经分配了* b $ b *:我发现这通常被称为一次 ; 分配。 我能用Python得到的最好的是: 尝试: 变量除了NameError之外的: 变量=方法() 我想知道sombody是否有解决方案(技巧,无论......)看起来更加紧凑的编码。类似的东西: 一次(变量,方法) 不起作用,但它会很完美。当然我可以预处理 Python代码,但是全Python解决方案会更方便。 有什么建议吗? Thx提前, Lorenzo 解决方案 为什么不在开始时分配一次并完成它? 如果没有愚蠢的话,我建议你重新考虑一下 问题的方法。 你的变量是否有默认值?如果没有,如果用户 没有设置它们并且你的代码试图引用它们会发生什么?如果用户没有设置值,那么你似乎是在试图应用默认值,当 正确的事情是在用户之前应用默认值*得到一个 机会做任何事情*。然后用户做出的任何更改都将覆盖默认值。 更好的是,如果它实用,就是提供你的功能一个 或更多函数,其定义可以设置关键字 参数的默认值。 问候 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 您可以使用属性来实现延迟评估。或者您可以依赖 命名约定: .... 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) ....返回值 .... .... def _calc_foo(self): ....打印计算foo ....返回42 .... 计算foo 42 42 Traceback(最近一次调用最后一次): 文件"< stdin>",第1行,< module> 文件"< stdin>",第5行,在__getattr__ 文件"< stdin>",第4行,在__getattr__ AttributeError:没有方法来计算属性''bar'' ''bar-value'' Peter Hello,I''ve been using Python for some DES simulations because we don''t needfull C speed and it''s so much faster for writing models. Duringcoding I find it handy to assign a variable *unless it has beenalready assigned*: I''ve found that this is often referred to as "once"assigment.The best I could come up with in Python is:try:variableexcept NameError:variable = method()I wonder if sombody has a solution (trick, whatever ...) which looksmore compact in coding. Something like:once(variable, method)doesn''t work, but it would be perfect. Of course I can preprocess thePython code but an all-Python solution would be more handy.Any suggestions?Thx in advance,Lorenzo 解决方案Why not just assign to it once at the beginning and be done with it?Without being fatuous, I would suggest you rethink your approach to theproblem.Do your variables have default values? If not, what happens if the userdoes not set them and your code tries to refer to them? You seem to betrying to apply defaults if the user hasn''t set a value, when thecorrect things to do is to apply the defaults *before the user gets achance to do anything at all*. Then any changes made by the user willoverwrite the defaults.Even better, if its practical, is to provide your functionality as oneor more functions, whose definitions can set default values for keywordarguments.regardsSteve--Steve Holden +1 571 484 6266 +1 800 494 3119Holden Web LLC/Ltd http://www.holdenweb.comSkype: holdenweb http://del.icio.us/steve.holdenSorry, the dog ate my .siglineYou can use properties to implement lazy evaluation. Or you can rely on anaming convention:.... 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........ def _calc_foo(self):.... print "calculating foo".... return 42....calculating foo4242Traceback (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''''bar-value''Peter 这篇关于&QUOT;一旦&QUOT; Python中的分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 16:10