python解释器能否重写函数失败? [英] Can the python interpreter fail on redeclared functions?

查看:358
本文介绍了python解释器能否重写函数失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个足够大的python文件上工作时,我不小心重新定义了一个函数在全局范围。如果python解释器可以在这些情况下警告我,我将不胜感激。



想象你从这个代码开始(版本1):

 #!/ usr / bin / env python 

...大量代码...

def foo(version):
if version == 1:
return正确
returnOops!

...更多代码...

print foo(1)

正常工作:

 正确

然后你想改变一些东西,并将其称为版本2.你重写了foo函数,但是你不知道老的存在,或者你忘记删除它。您最终会得到这个结果:

 #!/ usr / bin / env python 

def foo版本):
if version == 2:
return正确
return糟糕!

...很多代码...

def foo(version):
if version == 1:
returnCorrect
return糟糕!

...更多代码...

print foo(2)

这不太好:

 糟糕! 



我知道python允许这样的代码:

  def monkey():
returnpatching
monkey =is
def monkey():
return {really :fun}

但是似乎使用def / p>

有任何方式我可以得到这样的行为:

  #!/ usr / bin / env python --def-strict 
def foo():
pass
def foo():
pass

结果:

 最近调用最后):
文件...,第3行,在< module>
NameError:name'foo'已经定义


解决方案

你可以创建一个装饰器,它可以比较函数的名称,或者将它存储在字典中。如果键已经存在,你可以抛出异常的装饰器!在开发过程中用这个装饰器装饰所有的功能。



这样的东西。

 

#import sys

如果sys.argv [1] ==--def-strict:
def duplicateFinder ):
if globals()。has_key(f .__ name__):
raise AttributeError,此模块已经有一个函数%s已定义%f .__ name__
return f
else:
def duplicateFinder(f):
return f

@duplicateFinder
def myFunction():
printHello World!

@duplicateFinder
def myFunction():
printHello World Again !!!


当运行python --def -strict scriptname。



编辑:添加您的假设的--def-strict!此外,没有必要保留一个单独的__functionNames字典。 globals()字典就够了。所以编辑它以反映相同!


While working on a large enough python file, I have accidentally redefined a function in the global scope. I would appreciate it if the python interpreter could warn me in those cases.

Imagine you start with this code (version 1):

#!/usr/bin/env python

... lots of code ...    

def foo(version):
  if version == 1:
    return "Correct"
  return "Oops!"

... lots more code ...

print foo(1)

Which works properly:

Correct

And then you want to change some things, and call it version 2. You rewrite the foo function, but you either don't realize the old one existed, or you forget to delete it. You end up with this:

#!/usr/bin/env python

def foo(version):
  if version == 2:
    return "Correct"
  return "Oops!"

... lots of code ...    

def foo(version):
  if version == 1:
    return "Correct"
  return "Oops!"

... lots more code ...

print foo(2)

Which doesn't work so well:

Oops!

I know python allows code like this:

def monkey():
  return "patching"
monkey = "is"
def monkey():
  return {"really": "fun"}

But it seems like using "def" that way is poor practice.

Is there any way I can get this sort of behavior:

#!/usr/bin/env python --def-strict
def foo():
  pass
def foo():
  pass

Results in:

Traceback (most recent call last):
  File ..., line 3, in <module>
NameError: name 'foo' is already defined

解决方案

You can create a decorator, which can compare the name of the function and maybe store it in a dictionary. And if the key already exists, you can throw an exception from the decorator! Decorate all your functions with this decorator during development. You can then get rid of the decoration after all your testing is done!

Something like


#import sys

if sys.argv[1] == "--def-strict":
    def duplicateFinder(f):
        if globals().has_key(f.__name__):
            raise AttributeError, "This module already has a function %s defined" % f.__name__
        return f
else:
    def duplicateFinder(f):
        return f

@duplicateFinder
def myFunction():
    print "Hello World!"

@duplicateFinder
def myFunction():
    print "Hello World Again!!!"


This should throw an error when run with "python --def-strict scriptname".

EDIT: Adding your hypothetical --def-strict! Also, there is no need to keep a separate __functionNames dictionary. The globals() dictionary is good enough. So editing it to reflect the same!

这篇关于python解释器能否重写函数失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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