Python 如何在不调用初始值设定项的情况下创建类实例?
是否有任何方法可以避免在初始化类时(例如从类方法)对类调用Python 如何在不调用初始值设定项的情况下创建类实例?,python,class,constructor,initialization,Python,Class,Constructor,Initialization,是否有任何方法可以避免在初始化类时(例如从类方法)对类调用\uuuuu init\uuuu 我试图在Python中创建一个不区分大小写和标点符号的字符串类,用于有效的比较目的,但在创建一个新实例时,如果不调用\uuuuuu init\uuuu,就会遇到问题 >>> class String: def __init__(self, string): self.__string = tuple(string.split()) self.__
\uuuuu init\uuuu
我试图在Python中创建一个不区分大小写和标点符号的字符串类,用于有效的比较目的,但在创建一个新实例时,如果不调用\uuuuuu init\uuuu
,就会遇到问题
>>> class String:
def __init__(self, string):
self.__string = tuple(string.split())
self.__simple = tuple(self.__simple())
def __simple(self):
letter = lambda s: ''.join(filter(lambda s: 'a' <= s <= 'z', s))
return filter(bool, map(letter, map(str.lower, self.__string)))
def __eq__(self, other):
assert isinstance(other, String)
return self.__simple == other.__simple
def __getitem__(self, key):
assert isinstance(key, slice)
string = String()
string.__string = self.__string[key]
string.__simple = self.__simple[key]
return string
def __iter__(self):
return iter(self.__string)
>>> String('Hello, world!')[1:]
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
String('Hello, world!')[1:]
File "<pyshell#1>", line 17, in __getitem__
string = String()
TypeError: __init__() takes exactly 2 positional arguments (1 given)
>>>
将另一个参数传递给构造函数,如下所示:
def __init__(self, string, simple = None):
if simple is None:
self.__string = tuple(string.split())
self.__simple = tuple(self.__simple())
else:
self.__string = string
self.__simple = simple
你可以这样称呼它:
def __getitem__(self, key):
assert isinstance(key, slice)
return String(self.__string[key], self.__simple[key])
此外,我不确定是否允许同时命名字段和方法
\uu simple
。如果只是为了可读性,你应该改变这一点。如果可行,让\uuuu init\uuuu
被调用(并通过合适的参数使调用无害)是更好的选择。但是,如果这需要太多的扭曲,您确实可以选择,只要您避免使用旧样式类的灾难性选择(在新代码中使用旧样式类有很好的理由,没有也有一些很好的理由,不使用)…:
这个习惯用法通常用在classmethod
s中,这些方法是作为“替代构造函数”使用的,因此您通常会看到它的使用方式,例如…:
@classmethod
def makeit(cls):
self = cls.__new__(cls)
# etc etc, then
return self
(这样,当调用子类而不是基类时,classmethod将被正确继承并生成子类实例)。标准pickle和copy模块使用的一个技巧是创建一个空类,使用该类实例化对象,然后将该实例的
\uu class\uuuuuu
分配给“真实”类。e、 g
>>> class MyClass(object):
... init = False
... def __init__(self):
... print 'init called!'
... self.init = True
... def hello(self):
... print 'hello world!'
...
>>> class Empty(object):
... pass
...
>>> a = MyClass()
init called!
>>> a.hello()
hello world!
>>> print a.init
True
>>> b = Empty()
>>> b.__class__ = MyClass
>>> b.hello()
hello world!
>>> print b.init
False
但请注意,这种方法很少有必要。绕过
\uuuu init\uuuuu
可能会产生一些意想不到的副作用,特别是如果您不熟悉原始类,那么请确保您知道自己在做什么。在本例中,使用元类提供了一个很好的解决方案。元类的用途有限,但工作正常
>>> class MetaInit(type):
def __call__(cls, *args, **kwargs):
if args or kwargs:
return super().__call__(*args, **kwargs)
return cls.__new__(cls)
>>> class String(metaclass=MetaInit):
def __init__(self, string):
self.__string = tuple(string.split())
self.__simple = tuple(self.__simple())
def __simple(self):
letter = lambda s: ''.join(filter(lambda s: 'a' <= s <= 'z', s))
return filter(bool, map(letter, map(str.lower, self.__string)))
def __eq__(self, other):
assert isinstance(other, String)
return self.__simple == other.__simple
def __getitem__(self, key):
assert isinstance(key, slice)
string = String()
string.__string = self.__string[key]
string.__simple = self.__simple[key]
return string
def __iter__(self):
return iter(self.__string)
>>> String('Hello, world!')[1:]
<__main__.String object at 0x02E78830>
>>> _._String__string, _._String__simple
(('world!',), ('world',))
>>>
类MetaInit(类型):
定义调用(cls、*ARG、**kwargs):
如果是args或kwargs:
return super()
返回cls.\uuuu新建\uuuuu(cls)
>>>类字符串(元类=MetaInit):
定义初始化(self,字符串):
self.\uu string=tuple(string.split())
self.\uuu simple=元组(self.\uu simple())
定义简单(自):
letter=lambda s:''.join(筛选器(lambda s:'a'>字符串('Hello,world!'))[1:]
>>>_u.u字符串u字符串,u.u字符串u简单
(‘世界’,(‘世界’,))
>>>
附录:
六年后,我的观点比我自己的方法更倾向于元类。由于元类仍在脑海中,下面的答案说明了如何在有和没有元类的情况下解决这个问题:
#! /usr/bin/env python3
METHOD = 'metaclass'
class NoInitMeta(type):
def new(cls):
return cls.__new__(cls)
class String(metaclass=NoInitMeta if METHOD == 'metaclass' else type):
def __init__(self, value):
self.__value = tuple(value.split())
self.__alpha = tuple(filter(None, (
''.join(c for c in word.casefold() if 'a' <= c <= 'z') for word in
self.__value)))
def __str__(self):
return ' '.join(self.__value)
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return self.__alpha == other.__alpha
if METHOD == 'metaclass':
def __getitem__(self, key):
if not isinstance(key, slice):
raise NotImplementedError
instance = type(self).new()
instance.__value = self.__value[key]
instance.__alpha = self.__alpha[key]
return instance
elif METHOD == 'classmethod':
def __getitem__(self, key):
if not isinstance(key, slice):
raise NotImplementedError
instance = self.new()
instance.__value = self.__value[key]
instance.__alpha = self.__alpha[key]
return instance
@classmethod
def new(cls):
return cls.__new__(cls)
elif METHOD == 'inline':
def __getitem__(self, key):
if not isinstance(key, slice):
raise NotImplementedError
cls = type(self)
instance = cls.__new__(cls)
instance.__value = self.__value[key]
instance.__alpha = self.__alpha[key]
return instance
else:
raise ValueError('METHOD did not have an appropriate value')
def __iter__(self):
return iter(self.__value)
def main():
x = String('Hello, world!')
y = x[1:]
print(y)
if __name__ == '__main__':
main()
!/usr/bin/env python3
方法='元类'
类别NoInitMeta(类型):
def新(cls):
返回cls.\uuuu新建\uuuuu(cls)
类字符串(如果方法=='metaclass'else类型,则metaclass=NoInitMeta):
定义初始值(自身,值):
self.\uu value=tuple(value.split())
self.\uuu alpha=元组(过滤器(无、(
''.join(如果'a'我更喜欢你的第一个答案,那么在word.casefold()中用c代替c。考虑到str()=''
,我想我会把代码改成def\uuuu init\uuuuuuuuuuuuuuuuuuuuuuu(self,string='')
。谢谢你的帮助和想法!我本来希望避免\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
对于上述更改,操作成本应该不会太高。这似乎是目前最好的解决方案。对不起,我认为我的第一个答案并没有真正解决这个问题:)我似乎没有办法把它取回…?你会如何为Python 3.1重写它?如果String是一个子类,你会如何调用超类的构造函数?哦,我找到了它,你会在创建self
后执行superclass.\uuuu init\uuuuuuuuuuuuuu(self)
。可能的重复
>>> class MetaInit(type):
def __call__(cls, *args, **kwargs):
if args or kwargs:
return super().__call__(*args, **kwargs)
return cls.__new__(cls)
>>> class String(metaclass=MetaInit):
def __init__(self, string):
self.__string = tuple(string.split())
self.__simple = tuple(self.__simple())
def __simple(self):
letter = lambda s: ''.join(filter(lambda s: 'a' <= s <= 'z', s))
return filter(bool, map(letter, map(str.lower, self.__string)))
def __eq__(self, other):
assert isinstance(other, String)
return self.__simple == other.__simple
def __getitem__(self, key):
assert isinstance(key, slice)
string = String()
string.__string = self.__string[key]
string.__simple = self.__simple[key]
return string
def __iter__(self):
return iter(self.__string)
>>> String('Hello, world!')[1:]
<__main__.String object at 0x02E78830>
>>> _._String__string, _._String__simple
(('world!',), ('world',))
>>>
#! /usr/bin/env python3
METHOD = 'metaclass'
class NoInitMeta(type):
def new(cls):
return cls.__new__(cls)
class String(metaclass=NoInitMeta if METHOD == 'metaclass' else type):
def __init__(self, value):
self.__value = tuple(value.split())
self.__alpha = tuple(filter(None, (
''.join(c for c in word.casefold() if 'a' <= c <= 'z') for word in
self.__value)))
def __str__(self):
return ' '.join(self.__value)
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return self.__alpha == other.__alpha
if METHOD == 'metaclass':
def __getitem__(self, key):
if not isinstance(key, slice):
raise NotImplementedError
instance = type(self).new()
instance.__value = self.__value[key]
instance.__alpha = self.__alpha[key]
return instance
elif METHOD == 'classmethod':
def __getitem__(self, key):
if not isinstance(key, slice):
raise NotImplementedError
instance = self.new()
instance.__value = self.__value[key]
instance.__alpha = self.__alpha[key]
return instance
@classmethod
def new(cls):
return cls.__new__(cls)
elif METHOD == 'inline':
def __getitem__(self, key):
if not isinstance(key, slice):
raise NotImplementedError
cls = type(self)
instance = cls.__new__(cls)
instance.__value = self.__value[key]
instance.__alpha = self.__alpha[key]
return instance
else:
raise ValueError('METHOD did not have an appropriate value')
def __iter__(self):
return iter(self.__value)
def main():
x = String('Hello, world!')
y = x[1:]
print(y)
if __name__ == '__main__':
main()