如何在不调用初始化程序的情况下创建一个类实例?

43
有没有办法在初始化类时避免调用 __init__,比如从类方法中调用?
我试图创建一个Python中用于高效比较的不区分大小写和标点符号的字符串类,但是在创建新实例时无法避免调用 __init__
>>> 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)
>>> 
我应该用什么替换string = String(); string.__string = self.__string[key]; string.__simple = self.__simple[key]来初始化新对象的切片?
编辑:受下面答案的启发,初始化程序已经被编辑为快速检查无参数。
def __init__(self, string=None):
    if string is None:
        self.__string = self.__simple = ()
    else:
        self.__string = tuple(string.split())
        self.__simple = tuple(self.__simple())

4个回答

61

如果可能的话,最好让__init__被调用(通过合适的参数来使调用无害)。但是,如果这样需要过多的扭曲,你还有一种选择,只要避免使用旧式类的灾难性选择(在新代码中使用旧式类没有任何好处,而有几个好理由不要这样做)...

   class String(object):
      ...

   bare_s = String.__new__(String)

这个习语通常在被用作“替代构造函数”的classmethod中使用,因此你通常会看到它以以下方式使用:

@classmethod 
def makeit(cls):
    self = cls.__new__(cls)
    # etc etc, then
    return self

这样做将正确继承classmethod,并在子类上调用时生成子类实例,而不是在基类上调用时生成。


9
你想让我为Python 3.1重写这个代码吗? - Noctis Skytower
如果String是一个子类,你如何调用父类的构造函数? - gozzilli
哦,我找到了,你需要在创建“self”之后执行“SuperClass.init(self)”。 - gozzilli
哇,我已经使用Python好几年/几十年了,但直到现在才发现这个。一直在寻找这个功能“有一段时间” :) - Hugh Perkins

14

标准的pickle和copy模块使用的一个技巧是创建一个空类,使用该类实例化对象,然后将该实例的__class__赋值给“真正”的类。例如:

>>> 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

但请注意,这种方法很少必要。绕过__init__可能会产生一些意外的副作用,特别是如果您不熟悉原始类,请确保您知道自己在做什么。


2
使用元类在这个例子中提供了一个不错的解决方案。虽然元类的使用有限,但它可以很好地工作。
>>> 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',))
>>> 

补充:

经过六年的时间,我的观点更倾向于Alex Martelli的答案。考虑到元类,以下回答展示了如何在有和没有元类的情况下解决问题:

#! /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()

0

向构造函数传递另一个参数,像这样:

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])

此外,我不确定是否允许同时将字段和方法命名为__simple。仅出于可读性的考虑,您应该更改它。

我更喜欢你的第一个答案。考虑到 str() == '',我想我会将代码更改为 def __init__(self, string=''):。感谢您的帮助和想法!我本来希望完全避免使用 __init__,但是使用所提到的更改调用 String() 应该不会是一个非常昂贵的操作。这似乎是目前最好的解决方案。 - Noctis Skytower
抱歉,我觉得我的第一个回答并没有真正回答问题 :) 看起来我无法找回它...? - Thomas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接