Max*_*axB 169 python namedtuple python-2.7 python-decorators python-attrs
我看到像这样的模式
def __init__(self, x, y, z):
...
self.x = x
self.y = y
self.z = z
...
Run Code Online (Sandbox Code Playgroud)
经常使用更多参数.有没有一种好方法可以避免这种乏味的重复性?该类应该继承namedtuple吗?
gru*_*czy 108
免责声明:似乎有几个人担心提出这个解决方案,所以我将提供一个非常明确的免责声明.您不应该使用此解决方案.我只提供它作为信息,所以你知道语言能够做到这一点.其余的答案只是显示语言功能,而不是支持以这种方式使用它们.
将参数显式复制到属性中没有任何问题.如果你在ctor中有太多的参数,它有时被认为是代码气味,也许你应该把这些参数分成更少的对象.其他时候,它是必要的,它没有任何问题.无论如何,明确地做它是要走的路.
但是,既然你要问它是如何完成的(而不是它是否应该完成),那么一个解决方案就是:
class A:
def __init__(self, **kwargs):
for key in kwargs:
setattr(self, key, kwargs[key])
a = A(l=1, d=2)
a.l # will return 1
a.d # will return 2
Run Code Online (Sandbox Code Playgroud)
Sip*_*hor 89
保留签名的装饰器解决方案:
import decorator
import inspect
import sys
@decorator.decorator
def simple_init(func, self, *args, **kws):
"""
@simple_init
def __init__(self,a,b,...,z)
dosomething()
behaves like
def __init__(self,a,b,...,z)
self.a = a
self.b = b
...
self.z = z
dosomething()
"""
#init_argumentnames_without_self = ['a','b',...,'z']
if sys.version_info.major == 2:
init_argumentnames_without_self = inspect.getargspec(func).args[1:]
else:
init_argumentnames_without_self = tuple(inspect.signature(func).parameters.keys())[1:]
positional_values = args
keyword_values_in_correct_order = tuple(kws[key] for key in init_argumentnames_without_self if key in kws)
attribute_values = positional_values + keyword_values_in_correct_order
for attribute_name,attribute_value in zip(init_argumentnames_without_self,attribute_values):
setattr(self,attribute_name,attribute_value)
# call the original __init__
func(self, *args, **kws)
class Test():
@simple_init
def __init__(self,a,b,c,d=4):
print(self.a,self.b,self.c,self.d)
#prints 1 3 2 4
t = Test(1,c=2,b=3)
#keeps signature
#prints ['self', 'a', 'b', 'c', 'd']
if sys.version_info.major == 2:
print(inspect.getargspec(Test.__init__).args)
else:
print(inspect.signature(Test.__init__))
Run Code Online (Sandbox Code Playgroud)
Jor*_*ley 29
显性比隐含更好...所以你肯定可以使它更简洁:
def __init__(self,a,b,c):
for k,v in locals().items():
if k != "self":
setattr(self,k,v)
Run Code Online (Sandbox Code Playgroud)
更好的问题是你吗?
...如果你想要一个命名元组我会建议使用一个名字元组(记住元组有一些附加条件)...也许你想要一个有序的甚至只是一个字典...
A S*_*ipt 29
正如其他人所提到的那样,重复并不坏,但在某些情况下,一个命名元组可能非常适合这类问题.这避免了使用locals()或kwargs,这通常是一个坏主意.
from collections import namedtuple
# declare a new object type with three properties; x y z
# the first arg of namedtuple is a typename
# the second arg is comma-separated or space-separated property names
XYZ = namedtuple("XYZ", "x, y, z")
# create an object of type XYZ. properties are in order
abc = XYZ("one", "two", 3)
print abc.x
print abc.y
print abc.z
Run Code Online (Sandbox Code Playgroud)
我发现它的使用有限,但你可以像任何其他对象一样继承一个namedtuple(例如,续):
class MySuperXYZ(XYZ):
""" I add a helper function which returns the original properties """
def properties(self):
return self.x, self.y, self.z
abc2 = MySuperXYZ(4, "five", "six")
print abc2.x
print abc2.y
print abc2.z
print abc2.properties()
Run Code Online (Sandbox Code Playgroud)
ger*_*rit 20
为了扩展gruszczy答案,我使用了一种模式:
class X:
x = None
y = None
z = None
def __init__(self, **kwargs):
for (k, v) in kwargs.items():
if hasattr(self, k):
setattr(self, k, v)
else:
raise TypeError('Unknown keyword argument: {:s}'.format(k))
Run Code Online (Sandbox Code Playgroud)
我喜欢这种方法,因为它:
super().__init(...))X.__init__在Python 3.6之前,这无法控制属性的设置顺序,如果某些属性是具有访问其他属性的setter的属性,则可能会出现问题.
它可能会有所改进,但我是我自己代码的唯一用户,所以我不担心任何形式的输入卫生.也许一个AttributeError更合适.
zon*_*ndo 10
你也可以这样做:
locs = locals()
for arg in inspect.getargspec(self.__init__)[0][1:]:
setattr(self, arg, locs[arg])
Run Code Online (Sandbox Code Playgroud)
当然,您必须导入该inspect模块.
这是一个没有任何额外导入的解决方案.
一个小帮手功能使它更方便和可重复使用:
def auto_init(local_name_space):
"""Set instance attributes from arguments.
"""
self = local_name_space.pop('self')
for name, value in local_name_space.items():
setattr(self, name, value)
Run Code Online (Sandbox Code Playgroud)
您需要调用它locals():
class A:
def __init__(self, x, y, z):
auto_init(locals())
Run Code Online (Sandbox Code Playgroud)
a = A(1, 2, 3)
print(a.__dict__)
Run Code Online (Sandbox Code Playgroud)
输出:
{'y': 2, 'z': 3, 'x': 1}
Run Code Online (Sandbox Code Playgroud)
locals()如果您不想更改locals()使用此版本:
def auto_init(local_name_space):
"""Set instance attributes from arguments.
"""
for name, value in local_name_space.items():
if name != 'self':
setattr(local_name_space['self'], name, value)
Run Code Online (Sandbox Code Playgroud)
在 Python 3.7 中,您可以(ab)使用模块dataclass中提供的装饰器dataclasses。从文档中:
该模块提供了一个装饰器和函数,用于自动将生成的特殊方法(例如
__init__()和 )__repr__()添加到用户定义的类中。它最初是在 PEP 557 中描述的。这些生成的方法中使用的成员变量是使用 PEP 526 类型注释定义的。例如这段代码:
Run Code Online (Sandbox Code Playgroud)@dataclass class InventoryItem: '''Class for keeping track of an item in inventory.''' name: str unit_price: float quantity_on_hand: int = 0 def total_cost(self) -> float: return self.unit_price * self.quantity_on_hand除其他外,将添加一个
__init__()看起来像这样的:Run Code Online (Sandbox Code Playgroud)def __init__(self, name: str, unit_price: float, quantity_on_hand: int=0): self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand请注意,此方法会自动添加到类中:它不是在上面显示的 InventoryItem 定义中直接指定。
如果您的类又大又复杂,则使用dataclass. 我是在 Python 3.7.0 发布当天写这篇文章的,因此使用模式尚未确定。
处理这个(并避免了很多其他的样板)一个有趣的库ATTRS.例如,您的示例可以简化为此(假设调用该类MyClass):
import attr
@attr.s
class MyClass:
x = attr.ib()
y = attr.ib()
z = attr.ib()
Run Code Online (Sandbox Code Playgroud)
你甚至不需要一个__init__方法,除非它也做其他的东西.这是Glyph Lefkowitz的精彩介绍.
我0.02美元.它非常接近Joran Beasley的答案,但更优雅:
def __init__(self, a, b, c, d, e, f):
vars(self).update((k, v) for k, v in locals().items() if v is not self)
Run Code Online (Sandbox Code Playgroud)
此外,MikeMüller的答案(对我来说最好的答案)可以通过这种技术减少:
def auto_init(ns):
self = ns.pop('self')
vars(self).update(ns)
Run Code Online (Sandbox Code Playgroud)
auto_init(locals())来自你的电话__init__