Python:创建类实例而不调用初始化程序

Noc*_*wer 28 python constructor initialization class

有没有办法避免__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)
>>> 
Run Code Online (Sandbox Code Playgroud)

我应该用什么替换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())
Run Code Online (Sandbox Code Playgroud)

Ale*_*lli 34

在可行的情况下,让__init__被调用(并通过合适的参数使调用无害)是可取的.但是,如果需要太多的扭曲,你可以有一个替代方案,只要你避免使用旧式类的灾难性选择(没有充分的理由在新代码中使用旧式类,以及几个好的理由 ...):

   class String(object):
      ...

   bare_s = String.__new__(String)
Run Code Online (Sandbox Code Playgroud)

这个成语通常用在classmethods中,它们可以作为"替代构造函数"使用,所以你通常会看到它以......等方式使用:

@classmethod 
def makeit(cls):
    self = cls.__new__(cls)
    # etc etc, then
    return self
Run Code Online (Sandbox Code Playgroud)

(这样,classmethod将被正确地继承,并在子类而不是基类上调用时生成子类实例).

  • 你会如何为Python 3.1重写这个? (6认同)

Cer*_*rin 6

标准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
Run Code Online (Sandbox Code Playgroud)

但请注意,这种方法很少需要.绕过__init__可能有一些意想不到的副作用,特别是如果你不熟悉原始类,所以要确保你知道你在做什么.


Noc*_*wer 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',))
>>> 
Run Code Online (Sandbox Code Playgroud)

附录:

六年后,我的观点更倾向于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()
Run Code Online (Sandbox Code Playgroud)