首页 文章

如何在python类中替换变量?

提问于
浏览
0

我被指示定义一个带有3个变量的类:(看起来像这样)

class random(object):
    def __init__(self,a,b,c):
        self.a=a
        self.b=b
        self.c=c
        self.result= #calculations with a,b and c.
    def __str__(self):
        #functions so that self.result prints correctly

但是,我还必须在上面的类中编写另一个函数:

def read(#takes whatever parameter I need)
         #functions which I dont know how to write

这个函数应该允许我从另一个文件中读取(让我们称之为file.txt“),并将self.a,self.b和self.c替换为该文件中的信息,如果你知道我的意思吗?比如说:

itemA=random(1,2,3)

和file.txt包含三行:

6
7
8

运行以下内容:

itemA.read()

假设将参数中的1,2,3替换为6,7,8,那样就可以了

itemA=random(6,7,8)

我怎样才能做到这一点?我已经编写了随机读取和重现的部分(6,7,8),其中:

fileHandle=open('file.txt', 'r')
fileHandle.readlines()
#some codes that turn string into integer/floats
fileHandle.close()
random(#put integers in)

但是,如果不手动编写,我无法将itemA从随机(1,2,3)更新为随机(6,7,8):

itemA=itemA.read() #in python shell

btw itemA只是一个示例名称,它可以命名为任何东西,所以我不能在我的计算中实际使用itemA ...

1 回答

  • 2

    在类的方法内部,您可以通过在 self 上设置属性来操作属性,就像在 __init__ 方法中一样 .

    您需要做的就是在阅读后设置这些属性 . 您可能希望 read() 方法也采用文件名:

    def read(self, filename):
        with open(filename) as fh:
            self.a = int(fh.readline())
            self.b = int(fh.readline())
            self.c = int(fh.readline())
            self.result = # same calculation
    

    因此,当您调用 itemA.read() 时,它将改变属性;没有必要在这里返回任何东西 .

    您可以重用 __init__ 方法来设置这些属性,并避免重复 self.result 属性的计算:

    class random(object):
        def __init__(self, a, b, c):
            self.a = a
            self.b = b
            self.c = c
            self.result = #calculations with a,b and c.
    
        def read(self, filename):
            with open(filename) as fh:
                a = int(fh.readline())
                b = int(fh.readline())
                c = int(fh.readline())
                self.__init__(a, b, c)  # set the attributes plus result
    

    或者您可以在 __init__read() 之间共享一个单独的方法:

    class random(object):
        def __init__(self, a, b, c):
            self.a = a
            self.b = b
            self.c = c
            self._calculate_result()
    
        def _calculate_result(self)
            self.result = #calculations with self.a, self.b and self.c.
    
        def read(self, filename):
            with open(filename) as fh:
                self.a = int(fh.readline())
                self.b = int(fh.readline())
                self.c = int(fh.readline())
                self._calculate_result()  # set the result
    

    它会像这样工作:

    itemA = random(1, 2, 3)
    itemA.read('file.txt')
    # now itemA.a is set to 4, itemA.b is set to 5 and item.c is set to 6
    

    可能是您希望通过直接提供 abc 或通过从文件读取来生成类 random 的实例 . 这有点高级,但这意味着您提供了两种不同的工厂方法,两种不同的方式来生成 random 的实例 . 我倾向于使用类方法来实现额外的工厂方法:

    class random(object):
        def __init__(self, a, b, c):
            self.a = a
            self.b = b
            self.c = c
            self.result = #calculations with a,b and c.
    
        @classmethod
        def from_file(self, filename):
            with open(filename) as fh:
                a = int(fh.readline())
                b = int(fh.readline())
                c = int(fh.readline())
                return cls(a, b, c)  # create a new instance of this class
    

    我将 read 方法重命名为 from_file() ,以更好地记录这是一个工厂方法 . 你会像这样使用它:

    itemA = random(1, 2, 3)
    itemB = random.from_file('file.txt')
    

    这将创建两个单独的实例,一个实例直接提供值,另一个实例通过读取文件 .

相关问题