Skip to main content
 首页 » 编程设计

python之__init__ 方法在未传递任何值时获取值

2024年10月17日8soundcode

<分区>

我有一个类 TextBuffer。它有一个 __init__ 函数,可用于为 TextBuffer 的内容传递一个初始值。如果没有传递任何值,那么它应该使用一个空列表作为默认值。

该类有一个方法add,用于将文本添加到TextBuffer:

a = TextBuffer() 
a += "TEXT" 

由于某些原因,调用不带参数的__init__方法并没有为lst使用[]的默认值,而是之前添加的一个TextBuffer 的另一个实例的值,它不在 __init__ 调用中

a = TextBuffer() 
a += "BUFFER A" 
b = TextBuffer()  ### lst in the __init__ call has the value of ["BUFFER A"] 
b += "BUFFER B" 

我不知道为什么会这样。也许我在 __add__ 方法中做错了什么?

请考虑完整的示例代码:

import pprint 
 
### Two functions for byte/string conversion in Python3 
def b(x): 
    if ( type(x) == type(b'')): 
        return x 
    else: 
        return x.encode(encoding='UTF-8') 
def s(x): 
    if ( type(x) == type(b'') ): 
        return x.decode(encoding='UTF-8') 
    else: 
        return x 
 
 
class TextBuffer(): 
    def __init__( self, lst = [], maxlines = None ): 
        self.content = [] 
        if ( type(lst) == type([])): 
            if (len(lst) > 0): 
                print("INIT got a nonempty lst value:") 
                pprint.pprint(lst) 
                print("------------------------------") 
            self.content = lst 
 
 
    def __getitem__( self, i ): 
        try: 
            return self.content[i] 
        except IndexError: 
            return None 
 
    def __iter__( self ): 
        for line in self.content: 
            yield line 
 
    def __contains__( self, item ): 
        return item in self.content 
 
    def __len__( self ): 
        return len(self.content) 
 
    def __add__( self, item ): 
        self.content.append(item) 
        return self 
 
    def __radd__( self, item ): 
        return self.__add__(item) 
 
    def __repr__( self ): 
        result = "" 
        for line in self.content: 
            if ( type(line) == type(b"") ): 
                result+=s(line) 
            else: 
                result+=line 
        return result 
 
    def __str__( self ): 
        return repr(self) 
 
    def __unicode__( self ): 
        return repr(self.content) 
 
### TextBuffer INIT with empty list creates an empty textbuffer 
a = TextBuffer(lst=[]) 
print("a = TextBuffer(lst=[])") 
 
a += "BUFFER A" 
 
 
### TextBuffer INIT with empty list creates an empty textbuffer 
b = TextBuffer(lst=[]) 
print("b = TextBuffer(lst=[])") 
 
b += "BUFFER B" 
 
print("Content of TextBuffer a:") 
print(a) 
print("Content of TextBuffer b:") 
print(b) 
 
print("-------------------------") 
 
### TextBuffer INIT without any parameters should use default value for lst of [] 
### So an empty list. Should be the same as TextBuffer(lst=[]) 
a = TextBuffer() 
print("a = TextBuffer()") 
 
a += "BUFFER A" 
 
### TextBuffer INIT without any parameters should use default value for lst of [] 
### So an empty list. Should be the same as TextBuffer(lst=[]) 
### But now, the value of lst is not [], it is the string added to TextBuffer 'a': ['BUFFER A'] 
b = TextBuffer() 
print("b = TextBuffer()") 
 
b += "BUFFER B" 
 
print("Content of TextBuffer a:") 
print(a) 
print("Content of TextBuffer b:") 
print(b)