是否可以使用True初始化bool?

时间:2023-01-15 22:47:10

I am defining a variable through an empty declaration:

我通过一个空声明定义一个变量:

In [1]: a = bool()

In [2]: a
Out[2]: False

Is it possible to force an initialization of True?

是否可以强制初始化为True?


Note: I know I can simply have a = True or a = bool(True) but I will be initializing several variables with specific types, something like

注意:我知道我可以简单地使用= True或a = bool(True),但我将使用特定类型初始化几个变量,类似于

x = dict()
for v in [('a', int), ('b', float), ('c', bool)]
    x[v[0]] = v[1]()

The defaults for int, float, etc. are OK, I would just prefer to specifically have a bool initialized as True

int,float等的默认值都可以,我只想将bool初始化为True

3 个解决方案

#1


4  

You can use functools.partial to customize your bool:

您可以使用functools.partial来自定义您的bool:

from functools import partial

my_bool = partial(bool, 1)

print(my_bool())
# True

And you can use it in your code as:

您可以在代码中使用它:

x = dict()
for v in [('a', int), ('b', float), ('c', my_bool)]
    x[v[0]] = v[1]()

You could also use a lambda (credit: @tobias_k):

你也可以使用lambda(credit:@tobias_k):

my_bool = lambda: True

#2


2  

Maybe a possible solution for you with the flexibility for other special default values

也许是一个可能的解决方案,具有其他特殊默认值的灵活性

typeDefaults = {bool: True}

for v in [('a', int), ('b', float), ('c', bool)]:
    x[v[0]] = typeDefaults[v[1]] if v[1] in typeDefaults else v[1]()

#3


1  

You could map the types you want to change the default values for to your own functions:

您可以将要更改默认值的类型映射到您自己的函数:

>>> defaults = {bool: lambda: True}
>>> x = {}
>>> for k, v in [('a', int), ('b', float), ('c', bool)]:
...     x[k] = defaults.get(v, v)()
... 
>>> x
{'a': 0, 'c': True, 'b': 0.0}

or using a dictionary comprehension:

或使用词典理解:

>>> {k: defaults.get(v, v)() for k, v in [('a', int), ('b', float), ('c', bool)]}
{'a': 0, 'c': True, 'b': 0.0}

#1


4  

You can use functools.partial to customize your bool:

您可以使用functools.partial来自定义您的bool:

from functools import partial

my_bool = partial(bool, 1)

print(my_bool())
# True

And you can use it in your code as:

您可以在代码中使用它:

x = dict()
for v in [('a', int), ('b', float), ('c', my_bool)]
    x[v[0]] = v[1]()

You could also use a lambda (credit: @tobias_k):

你也可以使用lambda(credit:@tobias_k):

my_bool = lambda: True

#2


2  

Maybe a possible solution for you with the flexibility for other special default values

也许是一个可能的解决方案,具有其他特殊默认值的灵活性

typeDefaults = {bool: True}

for v in [('a', int), ('b', float), ('c', bool)]:
    x[v[0]] = typeDefaults[v[1]] if v[1] in typeDefaults else v[1]()

#3


1  

You could map the types you want to change the default values for to your own functions:

您可以将要更改默认值的类型映射到您自己的函数:

>>> defaults = {bool: lambda: True}
>>> x = {}
>>> for k, v in [('a', int), ('b', float), ('c', bool)]:
...     x[k] = defaults.get(v, v)()
... 
>>> x
{'a': 0, 'c': True, 'b': 0.0}

or using a dictionary comprehension:

或使用词典理解:

>>> {k: defaults.get(v, v)() for k, v in [('a', int), ('b', float), ('c', bool)]}
{'a': 0, 'c': True, 'b': 0.0}