如何动态更改数据类属性的默认值?
How to change default values of dataclass attributes dynamically?
我想知道是否可以(动态地)更改 dataclass
中的默认参数值以实现如下目的:
from dataclasses import dataclass
@dataclass
class Foo:
bar = "foobar"
Foo.bar = "world"
foo1 = Foo(bar="hello") # foo1.bar = "hello"
foo2 = Foo() # foo2.bar = "world"
您应该继承您的数据类:
from dataclasses import dataclass
@dataclass
class Foo:
bar = "foobar"
Foo.bar = "world"
foo1 = Foo(bar="hello") # foo1.bar = "hello"
foo2 = Foo() # foo2.bar = "world"
from dataclasses import dataclass
@dataclass
class NewFoo(Foo):
bar = "world"
foo1 = Foo(bar="hello") # foo1.bar = "hello"
foo2 = NewFoo() # foo2.bar = "world"
我想知道是否可以(动态地)更改 dataclass
中的默认参数值以实现如下目的:
from dataclasses import dataclass
@dataclass
class Foo:
bar = "foobar"
Foo.bar = "world"
foo1 = Foo(bar="hello") # foo1.bar = "hello"
foo2 = Foo() # foo2.bar = "world"
您应该继承您的数据类:
from dataclasses import dataclass
@dataclass
class Foo:
bar = "foobar"
Foo.bar = "world"
foo1 = Foo(bar="hello") # foo1.bar = "hello"
foo2 = Foo() # foo2.bar = "world"
from dataclasses import dataclass
@dataclass
class NewFoo(Foo):
bar = "world"
foo1 = Foo(bar="hello") # foo1.bar = "hello"
foo2 = NewFoo() # foo2.bar = "world"