如何访问 python 数据类中默认参数中的另一个参数?
How do I access another argument in a default argument in a python dataclass?
我正在尝试从 name
导出 id_
的默认值,反之亦然。
@dataclass
class Item:
id_ = NAME_TO_ID[name]
name = ID_TO_NAME[id_]
我应该可以这样调用 class:
Item(id_=123)
Item(name='foo')
如果可能的话,我也希望 class 在同时提供 id_
和 name
时引发错误。
Item(id_=123, name='foo') # ValueError: id_ and name cannot be provided together
关于我应该如何做这件事有什么建议吗?
像下面这样简单的事情对你有用吗?
在对象实例化时,检查提供的数据是过多还是过少,然后定义一个 属性 来计算必要时的值?
class Item ():
def __init__(self, id: int =None, name:str= None):
if all ([name, id]):
raise ValueError ("id_ and name cannot be provided together")
elif not any ([name, id]):
raise ValueError ("name or id must be provided for Item instantiation")
else:
self._name = name
self._id = id
@property
def name (self) -> str:
if self._name is None:
#Compute the value and return it
pass #remove this once you figure out your algorithm
else:
return self._name
@property
def id (self) ->int:
if self._id is None:
#Compute the value and return it
pass #remove this once you figure out your algorithm
else:
return self._id
请注意,您还必须考虑什么是有效值。在我提供的示例中,将整数 0
视为有效 id
并将空字符串 ""
视为有效 name
.
是不够的
您可以使用编写 __post_init__
方法来进行这些验证
from dataclasses import dataclass, field
@dataclass
class Item:
id_: int = field(default=None)
name: str = field(default=None)
def __post_init__(self):
if self.id_ is None and self.name is None:
raise TypeError("You must provide exactly one of name or id_")
if self.id_ is not None and self.name is not None:
raise TypeError("You must provide exactly one of name or id_")
if self.id_ is not None:
self.name = id_to_name(self.id_)
else:
self.id_ = name_to_id(self.name)
您需要使用 class
的 __init__
功能。
例如,
class Item:
# define __init__ such that it has a condition when both id_ and name are supplied
# a ValueError is raised
def __init__(self, id_, name=None):
if (id_ and name):
# raise error because both were supplied
raise ValueError
if (id_):
# assign name and id
elif (name):
# assign name and id
不过,在这里,用户必须为两者传递一个值。您可以简单地提供 False
或 None
或一些虚假值,以便它被传递并且不会抛出 ValueError。
我正在尝试从 name
导出 id_
的默认值,反之亦然。
@dataclass
class Item:
id_ = NAME_TO_ID[name]
name = ID_TO_NAME[id_]
我应该可以这样调用 class:
Item(id_=123)
Item(name='foo')
如果可能的话,我也希望 class 在同时提供 id_
和 name
时引发错误。
Item(id_=123, name='foo') # ValueError: id_ and name cannot be provided together
关于我应该如何做这件事有什么建议吗?
像下面这样简单的事情对你有用吗?
在对象实例化时,检查提供的数据是过多还是过少,然后定义一个 属性 来计算必要时的值?
class Item ():
def __init__(self, id: int =None, name:str= None):
if all ([name, id]):
raise ValueError ("id_ and name cannot be provided together")
elif not any ([name, id]):
raise ValueError ("name or id must be provided for Item instantiation")
else:
self._name = name
self._id = id
@property
def name (self) -> str:
if self._name is None:
#Compute the value and return it
pass #remove this once you figure out your algorithm
else:
return self._name
@property
def id (self) ->int:
if self._id is None:
#Compute the value and return it
pass #remove this once you figure out your algorithm
else:
return self._id
请注意,您还必须考虑什么是有效值。在我提供的示例中,将整数 0
视为有效 id
并将空字符串 ""
视为有效 name
.
您可以使用编写 __post_init__
方法来进行这些验证
from dataclasses import dataclass, field
@dataclass
class Item:
id_: int = field(default=None)
name: str = field(default=None)
def __post_init__(self):
if self.id_ is None and self.name is None:
raise TypeError("You must provide exactly one of name or id_")
if self.id_ is not None and self.name is not None:
raise TypeError("You must provide exactly one of name or id_")
if self.id_ is not None:
self.name = id_to_name(self.id_)
else:
self.id_ = name_to_id(self.name)
您需要使用 class
的 __init__
功能。
例如,
class Item:
# define __init__ such that it has a condition when both id_ and name are supplied
# a ValueError is raised
def __init__(self, id_, name=None):
if (id_ and name):
# raise error because both were supplied
raise ValueError
if (id_):
# assign name and id
elif (name):
# assign name and id
不过,在这里,用户必须为两者传递一个值。您可以简单地提供 False
或 None
或一些虚假值,以便它被传递并且不会抛出 ValueError。