pep 336:使无可调用 [英] pep 336: Make None Callable

查看:63
本文介绍了pep 336:使无可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PEP:336

标题:无可赎回

版本:$修订版:1.1 $

最后修改日期:$ Date:2004 / 11/03 16:58:30 $

作者:Andrew McClelland

状态:草稿

类型:标准追踪

内容类型:text / plain

创建时间:2004年10月28日

历史后:

摘要


没有应该是一个可调用的对象,当用任何

参数调用时没有副作用并且返回None。

Motivation


根据Python语言的简约函数编程目标

,允许可选动作的编程风格更多



基本原理


允许在方法表中使用None作为通用无效

而不是(1)检查方法表条目在调用之前没有

,或者(2)用类似于其他函数的

参数写一个本地无效方法在表中。


语义将是有效的,


类无:


def __call __(self,* args):

pass

如何使用


之前,检查函数表条目对无:


class选择:


def a(自我,输入):

打印''a''


def b(自我,输入):

打印''b''


def c(自我,输入);

打印''c''


def __call __(自我,输入):

function = {1:self .a,

2:self.b,

3:self.c

} .get(输入,无)

if function:return函数(输入)


之前,使用本地无效方法:


class选择:


def a(自我,输入):

打印''a''


def b(自我,输入):

打印''b''


def c(自我,输入);

打印''c''


def nop(自我,输入):

通过


def __call __(自我,输入):

返回{1:self.a,

2:self.b,

3:self.c

} .get(输入, self.nop)(输入)


之后:


class选择:


def a (自我,输入):

打印''''

def b(自我,输入):

print' 'b''


def c(自我,输入);

打印''c''


def __call __(自我,输入):

返回{1:self.a,

2:self.b,

3:self。 c

} .get(输入,无)(输入)

参考资料


[1] Python参考手册,第3.2节,
http://docs.python.org/ref/ref .html

版权


本文档已被置于公共领域。


Local变量:

模式:缩进文本

indent-tabs-mode:nil

句子结束双倍空间:t

填充栏:70

结束:

PEP: 336
Title: Make None Callable
Version: $Revision: 1.1 $
Last-Modified: $Date: 2004/11/03 16:58:30 $
Author: Andrew McClelland
Status: Draft
Type: Standards Track
Content-Type: text/plain
Created: 28-Oct-2004
Post-History:
Abstract

None should be a callable object that when called with any
arguments has no side effect and returns None.
Motivation

To allow a programming style for selectable actions that is more
in accordance with the minimalistic functional programming goals
of the Python language.
Rationale

Allow the use of None in method tables as a universal no effect
rather than either (1) checking a method table entry against None
before calling, or (2) writing a local no effect method with
arguments similar to other functions in the table.

The semantics would be effectively,

class None:

def __call__(self, *args):
pass
How To Use

Before, checking function table entry against None:

class Select:

def a(self, input):
print ''a''

def b(self, input):
print ''b''

def c(self, input);
print ''c''

def __call__(self, input):
function = { 1 : self.a,
2 : self.b,
3 : self.c
}.get(input, None)
if function: return function(input)

Before, using a local no effect method:

class Select:

def a(self, input):
print ''a''

def b(self, input):
print ''b''

def c(self, input);
print ''c''

def nop(self, input):
pass

def __call__(self, input):
return { 1 : self.a,
2 : self.b,
3 : self.c
}.get(input, self.nop)(input)

After:

class Select:

def a(self, input):
print ''a''

def b(self, input):
print ''b''

def c(self, input);
print ''c''

def __call__(self, input):
return { 1 : self.a,
2 : self.b,
3 : self.c
}.get(input, None)(input)
References

[1] Python Reference Manual, Section 3.2,
http://docs.python.org/ref/ref.html
Copyright

This document has been placed in the public domain.

Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
End:

推荐答案

修订版:1.1




最后修改:

Last-Modified:


日期:2004/11/03 16:58:30
Date: 2004/11/03 16:58:30


这篇关于pep 336:使无可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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