我有一个考虑错误的程序,请帮助我

Python函数oop继承方法

我想将openfiles方法的值用于sanitize方法。我在以下代码中使用了类和oop方法。请检查我的电话是否正确,或者是否还有其他错误。

import pickle
class Athlete(object):
    time1=[]
    t3=[]
    def __init__(self):
        pass
    def openfiles(self,filename):
        self.sanitize()
        self.f2=filename
        with open('C:/Users/M/Desktop/studiocode/hfpy_ch6_data/'+str(self.f2),"r") as self.f1:
            self.t1=self.f1.read()
            self.t2=self.t1.strip().split(",")
            print(self.t2)
            self.name=self.t2.pop(0)
            self.dob=self.t2.pop(0)
            self.time=self.t2            
            print("Name -:",self.name)
            print("Date of Birth -:",self.dob)
            print("Time Recorded -:",self.time)

    def sanitize(self):
        print("hi there")
        splitter=''
        print("hi1")
        if "-" in self.time:
            print("hi")
            splitter="-"
        elif ":" in self.time:
            splitter=":"
        else:
            print("hi3")
            return(self.time)
        (mins,sec)=self.time.split(splitter)
        self.t3=(mins+"."+sec)
        print("im here",self.t3)
        print(self.name)

    def top3(self):
        for self.i in self.t3:
            self.time1.append(self.i)
        print(self.time1)

b=Athlete()
x=b.openfiles("Sarah2.txt")
y=b.sanitize(x)
评论
  • 若初見
    若初見 回复

    我没有看实际的实现,但是仅从方法签名来看,正确的用法就是

    b = Athlete()
    b.openfiles("Sarah2.txt")
    b.sanitize()
    

    Neither your openfiles nor sanitize methods return anything, so assigning them to x and y will not be of use to you. Note that in class methods the self argument is implicitly passed as the first argument in this case (as long as the method is not decorated @staticmethod).