简体   繁体   English

python中逐步操作对象的最佳类结构实践

[英]Best class structure practice for step by step object manipulation in python

Hi I want to create a class with multiple methods. 嗨,我想用多种方法创建一个类。 Each method depends on the result of another method. 每种方法取决于另一种方法的结果。 A simplified example looks like: 一个简化的示例如下所示:

class klass(object):
def __init__(self, x, y):
    self.x = x
    self.y = y
    self.results = 0

def method1(self):
    self.results = self.x + self.y
    return self.results

def method2(self):
    self.results = math.sqrt(self.results)
    return self.results

method2 should called only after method1 . method2应该仅在method1之后调用。 ie: 即:

>>> b=klass(3.0, 4.0)
>>> b.method1()
7.0
>>> b.method2()
2.6457513110645907

The idea is that someone may need only the result of method1 and someone else may need the result of method2 as well. 这个想法是,某人可能只需要method1的结果,而其他人也可能需要method2的结果。 So the first person needs to run only method1 and the second person needs to run method1 and then method2 . 因此,第一人称仅需要运行method1 ,第二人称需要运行method1 ,然后运行method2

What is the best(most readable and simplest) way to do that. 什么是最好的(最易读和最简单)的方法。

Note that this is a much more simple version of a complex class 请注意,这是复杂类的简单得多的版本

I would switch results to be a dictionary and save the results for each level separately. 我会将结果切换为字典,然后分别保存每个级别的结果。 Then you can check in each method if "level1" not in self.results: raise an exception but then you can still provide the output of each level to users. 然后,您可以检入每个方法中的if "level1" not in self.results: raise an exception但仍可以将每个级别的输出提供给用户。 Furthermore you could start each method by checking to see if the results exist already. 此外,您可以通过检查结果是否已经存在来启动每种方法。 An example would be like so: 一个例子是这样的:

class klass(object):
def __init__(self, x, y):
    self.x = x
    self.y = y
    self.results = {"level0": 0}

def method1(self):
    if "level1" not in self.results:
        self.results["level1"] = self.x + self.y
    return self.results["level1"]

def method2(self):
    if "level1" not in self.results:
        raise ValueError("The results of method1 are not yet available")
    if "level2" not in self.results:
        self.results["level2"] = math.sqrt(self.results["level1"])
    return self.results["level2"]

PS Don't use the name klass PS不要使用klass这个名字

Correct me if I'm getting this wrong, but couldn't you just call method1 in method2 like this: 如果我弄错了,请更正我,但是您不能像这样在method2中仅调用method1:

def method2(self):
    self.method1()
    self.results = math.sqrt(self.results)
    return self.results

You can write separate functions to aggregate functions you need in a single place: 您可以编写单独的函数以在单个位置聚合所需的函数:

class klass(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.results = 0

    def method1(self):
        self.results = self.x + self.y
        return self.results

    def method2(self):
        self.results = math.sqrt(self.results)
        return self.results

    def both_methods(self):
        self.method1()
        self.method2()
        return self.results

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM