[英]How to pass a constant variable to a function
which one is a better design in the following scenario and why?在以下场景中,哪一个是更好的设计,为什么?
A: A:
stop_words = ['com1', 'com2']
def clean_text(text_tokens, stop_words):
return [token for token in text_tokens if token not in stop_words]
clean_text(['hello', 'world', 'com1', 'com2'], stop_words)
B:乙:
def clean_text(text_tokens):
stop_words = ['com1', 'com2']
return [token for token in text_tokens if token not in stop_words]
clean_text(['hello', 'world', 'com1', 'com2'])
C: C:
STOP_WORDS = ['com1', 'com2']
def clean_text(text_tokens):
return [token for token in text_tokens if token not in STOP_WORDS]
clean_text(['hello', 'world', 'com1', 'com2'])
Added C version based on @MisterMiyagi answer.添加了基于@MisterMiyagi 回答的 C 版本。
Note1: In this context, stop_words is fixed and does not change.注1:在这种情况下,stop_words 是固定的,不会改变。
Note2: stop_words can be a small or a very large list.注2:stop_words 可以是一个很小的列表,也可以是一个很大的列表。
Middle ground: use a default value for the argument.中间立场:为参数使用默认值。
def clean_text(text_tokens, stop_words={'com1', 'com2'}):
return [token for token in text_tokens if token not in stop_words]
clean_text(['hello', 'world', 'com1', 'com2'])
Now the constant {'com1', 'com2'}
is only created once (when the function is defined);现在常量
{'com1', 'com2'}
只创建一次(在定义函数时); it doesn't pollute the global scope;它不会污染全局范围; and if you end up wanting to, you can optionally pass different
stop_words
when you call clean_text
.如果你最终想,你可以选择通过不同的
stop_words
当你调用clean_text
。
Prefer to create constants at global scope.更喜欢在全局范围内创建常量。 The global scope is evaluated once , whereas function-local scope is evaluated on each function call .
全局作用域被评估一次,而函数局部作用域在每次函数调用时被评估。
For very large searches, prefer to use a set
due to its O(1) lookup, versus the list
O(n) lookup.对于非常大的搜索,由于其 O(1) 查找而不是
list
O(n) 查找,因此更喜欢使用set
。 Values that are intended as constants should be named with ALL_CAPS_NAMES .用作常量的值应使用 ALL_CAPS_NAMES 命名。 Functions should directly reference constants iff they are not meant to be replaced.
函数应该直接引用常量,如果它们不打算被替换。
STOP_WORDS = {'com1', 'com2'} # constant set of words
def clean_text(text_tokens):
return [token for token in text_tokens if token not in STOP_WORDS]
# directly access constant ^
clean_text(['hello', 'world', 'com1', 'com2'])
For small constants, it may be advantageous to provide them as a literal.对于小常量,将它们作为文字提供可能更有利。 Even CPython is able to optimise inline literals to actual constants.
甚至 CPython 也能够将内联文字优化为实际常量。
def clean_text(text_tokens):
return [
token
for token in text_tokens
if token not in {'com1', 'com2'}
# ^ compiled as LOAD_CONST (frozenset({'com2', 'com1'}))
]
clean_text(['hello', 'world', 'com1', 'com2'])
The current optimiser converts list
and tuple
literals to tuple
constants, and set
and frozenset
literals to frozenset
constants当前优化器将
list
和tuple
文字转换为tuple
常量,并将set
和frozenset
文字转换为frozenset
常量
Scenario A is better if you want to pass different lists for stop_words
as a parameter each time you call the function, while Scenario B only test it for ['com1','com2']
,which mean you only change this list when you edit the function itself.如果您希望每次调用函数时都将不同的
stop_words
列表作为参数传递,则方案 A 更好,而方案 B 仅测试['com1','com2']
,这意味着您仅在编辑时更改此列表函数本身。
In conclusion: Scenario A is better to test different lists and pass them as parameters in the function.总结:场景 A 最好测试不同的列表,并在函数中将它们作为参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.