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

时间:2010-01-30 18:41:37

标签: python class constructor initialization

在初始化类时,有没有办法避免在类上调用__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())

5 个答案:

答案 0 :(得分:28)

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

   class String(object):
      ...

   bare_s = String.__new__(String)

这个习惯用法通常用于classmethod s,它们可以作为“替代构造函数”使用,所以你通常会看到它以......等方式使用:

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

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

答案 1 :(得分:7)

标准pickle和copy模块使用的一个技巧是创建一个空类,使用它来实例化对象,然后将该实例的__class__分配给“real”类。 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

但请注意,这种方法很少需要。绕过__init__会产生一些意想不到的副作用,特别是如果你不熟悉原始课程,那么请确保你知道你在做什么。

答案 2 :(得分: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's answer。由于元课程仍然存在,下面的答案显示了如何在有或没有它们的情况下解决问题:

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

答案 3 :(得分: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。如果仅为了可读性,您应该更改它。

答案 4 :(得分:-1)

在函数顶部使用@staticmethod

例如:

class TestClass:
    def __init__(self, a):
        print(1)
        self.arg = a
    @staticmethod
    def test(name):
        print(name)
TestClass.test('God')