python重写解释器_python解释器能否重写函数失败?

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

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值