Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何消除冗余? 类文档: 定义初始化(self,x:str,y:int): self.name=x self.value=y @类方法 def in_progress_document(cls):#尚未命名的文档 返回cls(“未命名”,0) @类方法 def unknown_文档(cls):#名称未知的文档 返回cls(“未知”,0)_Python_Oop - Fatal编程技术网

Python 如何消除冗余? 类文档: 定义初始化(self,x:str,y:int): self.name=x self.value=y @类方法 def in_progress_document(cls):#尚未命名的文档 返回cls(“未命名”,0) @类方法 def unknown_文档(cls):#名称未知的文档 返回cls(“未知”,0)

Python 如何消除冗余? 类文档: 定义初始化(self,x:str,y:int): self.name=x self.value=y @类方法 def in_progress_document(cls):#尚未命名的文档 返回cls(“未命名”,0) @类方法 def unknown_文档(cls):#名称未知的文档 返回cls(“未知”,0),python,oop,Python,Oop,在前面的代码中,两类方法之间是否存在冗余?如果是,如何以干净的方式将其移除 PS:我真的需要区分未命名和未知-未命名是一个正在进行的文档,可以稍后命名,但是未知是一个没有标题的完成文档,将来不会有标题。希望我正确理解您的问题,不是应该是这样吗 class Document: def __init__(self, x:str, y:int): self.name = x self.value = y @classmethod def in

在前面的代码中,两类方法之间是否存在冗余?如果是,如何以干净的方式将其移除


PS:我真的需要区分
未命名
未知
-
未命名
是一个正在进行的文档,可以稍后命名,但是
未知
是一个没有标题的完成文档,将来不会有标题。

希望我正确理解您的问题,不是应该是这样吗

class Document:

    def __init__(self, x:str, y:int):
        self.name = x
        self.value = y

    @classmethod
    def in_progress_document(cls, val): #a document that is not yet named
        return cls("unnamed", val) # value instead of 0 --> to avoid redundancy?

    @classmethod   
    def unknown_document(cls, val) : #a document whose name is unknown 
        return cls("unknown", val)
然后创建对象,如

normal_doc = Document('I am Normal', 100)
not_yet_named = Document.in_progress_document(10)
no_title_doc = Document.unknown_document(100) 

在下面的代码中,我插入了一个新的单类方法baseConstructor。in_progress_document和unknown_document现在是静态方法,没有任何参数。它们调用baseConstructor并传递所需的名称

class Document:

    def __init__(self, x:str, y:int):
        self.name = x
        self.value = y

    @classmethod
    def baseConstructor(cls, msg):
        return cls(msg, 0)

    @staticmethod
    def in_progress_document(): #a document that is not yet named
        return Document.baseConstructor("unnamed")

    @staticmethod   
    def unknown_document(): #a document whose name is unknown 
        return Document.baseConstructor("unknown")

您可能可以使用枚举或其他东西,但我个人认为这里没有任何问题。也许您在“错误代码”中传递的另一类方法是让您理解“您可能在“错误代码”中传递的另一类方法来保持事物简洁吗?”为什么您会认为这更可读和更少冗余?我想这些应该仍然是类方法,调用<代码> CLS。然后,您只能重写任何子类中的
baseConstructor
。我可能会选择名称
zeroed(cls,msg)
或其他什么。嗯,我的重点是冗余,而不是可读性。但是,调用方法的方式仍然是一样的。此外,cls(msg,0)只出现一次,msg是变化的部分(值0不重复)。因为只有两个字段,所以不容易实现。但是,如果你有更多的领域,那么它会更清楚。