[英]how to print the default value if argument is None in python
[sample.py] [示例.py]
def f(name='Hello Guest'):
print(name)
def A(name=None):
f(name)
A()
Expected Output: 'Hello Guest'预期输出:“你好客人”
Current Output: None电流输出:无
I'm expecting the answers by without using much more codes like ' name = name if name is not None else some_default_value
'我期待答案,无需使用更多代码,例如“
name = name if name is not None else some_default_value
”
Thanks in advance!提前致谢!
The best way to do this will be to use a shared default: 执行此操作的最佳方法是使用共享默认值:
DEFAULT_NAME = "Hello Guest"
def f(name=DEFAULT_NAME):
print(name)
def A(name=DEFAULT_NAME):
f(name)
Does this work for you? 这对你有用吗?
def f(name):
print(name or 'Hello Guest')
def A(name=None):
f(name)
A()
Out: "Hello Guest"
A("Hello World")
Out: "Hello World"
If the name variable is being used multiple times in the function, you could just reassign it in the beginning of the function. 如果在函数中多次使用name变量,则可以在函数的开头重新分配它。
name = name or "Hello Guest"
Using inspect.signature
to store default is one way to go: 使用
inspect.signature
存储默认值是一种方法:
def f(name='Hello Guest'):
print(name or inspect.signature(f).parameters['name'].default)
def A(name=None):
f(name)
A()
# Hello Guest
With some loss of generality, but simpler (shorter and no other lib): 有些失去一般性,但更简单(更短,没有其他lib):
def f(name='Hello Guest'):
print(name or f.__default__[0])
def A(name=None):
f(name)
A()
# Hello Guest
I have been in a similar situation where I can't change the signature or the body of a function I call internally but I want to use the defaults or pass arguments only when they exists(which can get tricky if you plan to pop those keys manually)我遇到过类似的情况,我无法更改我在内部调用的函数的签名或主体,但我想使用默认值或仅在它们存在时传递参数(如果你打算弹出这些键,这可能会变得很棘手手动)
This was the cleanest and the most reusable solution I could write.这是我能写的最干净、最可重用的解决方案。
def f(name='Hello Guest'):
print(name)
def A(**kwargs):
# If kwargs has any keys which are None in value, will be removed.
# Keys which don't exists in kwargs won't propagate, hence using default value.
f(**{k:v for k, v in kwargs.items() if v})
A()
# This returns "Hello Guest"
A(**{"name": None})
# This returns "Hello Guest"
A(**{"name": "Someone!"})
# This returns "Someone!"
Inspect is a great module if you plan to do something complex with function signature, parameters, etc.如果你打算用函数签名、参数等做一些复杂的事情,Inspect 是一个很棒的模块。
from inspect import signature
# This function is untouched
def f(name='Hello Guest'):
print(name)
# changed the signature so that params can propagate further
def A(**kwargs):
t = signature(f, follow_wrapped=True)
# If kwargs has any key which is None in value,
# it will be replaced with default values for the function.
# Keys which don't exists in kwargs won't propagate.
f(**{k: (v or t.parameters[k].default) for k, v in kwargs.items()})
A()
# This returns "Hello Guest"
A(**{"name": None})
# This returns "Hello Guest"
A(**{"name": "Someone!"})
# This returns "Someone!"
Or 要么
def f(name):
print(name)
def A(name = 'Hello Guest'):
f(name)
A()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.