12.19---python

发布时间 2023-12-20 09:59:27作者: 握嫩
seek()方法语法如下:
file.seek(offset[,whece])

offset - - 开始的偏移量,也就是代表需要移动偏移的字节数,如果是负数表示从倒数第几位开始。

whence:可选,默认值为0。给offset定义一个参数,表示要从那个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。

#返回值:如果操作成功,则返回新的文件位置,如果操作失败,则函数返回-1。

 

以下演示了seek()方法的使用:

f = open('workfile.txt','rb+')
f.write(b'0123456789abcdef')    #该文件有16个字节
print(f.seek(5))        #移动到文件的第六个字节

因为0代表的是开头,从0开始算5个字节后,第六个字节就是新的‘开头’,所以打印的结果是5

print(f.seek(5))
print(f.read(1))

在偏移量移动后,现在读取第一个字节就是   b'5'

print(f.seek(-3,2))    #移动到文件倒数第三个字节
print(f.read(1))

因为是负数,所以要从倒数第三个字节开始,而文本有16个字节,倒数带三个字节前面还有13个字节,因此f.seek(-3,2)打印的结果是13。

其次从倒数第三个字节开始算的第一个字节是‘d',因此f.read(1)得出的结果是b'd'。

 

www.runoob1.com
www.runoob2.com
www.runoob3.com
www.runoob4.com
www.runoob5.com

文件runoob.txt的内容如上

 

#打开文件
fo = open('runoob.txt','r+')
print('文件名为:',fo.name)

line = fo.readline()
print('读取的数据为:%s' % (line))

#重新设置文件读取指针到开头
fo.seek(0,0)
line = fo.readline()
print('读取的数据为;%s' %(line))

#关闭文件
fo.close()

以上两个得到的实例结果一致

 

 有趣的是当你输入并打印两次line的表达式以及结果的话会发生什么呢

 

fo.seek(0,0)
line = fo.readline()
print('读取的数据为;%s' %(line))
line = fo.readline()
print('读取的数据为;%s' %(line))

 

它得到的结果是:

 

类的继承:

class Car:                                    #1
    """一次模拟汽车的简单尝试"""
    
    def __init__(self,make,model,year):
        """初始化描述汽车的属性"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0
        
    def descriptive_name(self):
        """返回整洁的描述性信息"""
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()
    
    def read_odometer(self):
        """打印一条指出汽车里程的信息"""
        print(f"This car has {self.odometer_reading} miles on it.")
        
    def update_odmeter(self,mileage):
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You can't roll back an odometer!")
            
    def increment_odometer(self,miles):
        self.odometer_reading += miles
    
class ElectricCar(Car):                          #2
    """电动汽车的独特之处"""
    
    def __init__(self,make,model,year):                #3
        """初始化父类的属性"""
        super().__init__(make,model,year)              #4
    
my_tesla = ElectricCar('雅迪','model s ','2023')          #5
print(my_tesla.descriptive_name())

#1.创建子类时,父类必须包含在当前文件中,且位于子类前面

#2.定义了子类ElectricCar。定义子类时,必须在圆括号内指定父类的名称。

#3.方法__init__()接受创建Car实例所需的信息。

#4.super()是一个特殊的函数,让你能够调用父类的方法。调用Car类的方法__init__(),让ElectricCar实力包含这个方法中定义的所有属性。父类也被称为超类。

#5.创建ElectricCar类的一个实例,并将其赋给变量my_tesla。

 

class Car:
    --snip--
    
class ElectricCar(Car):
    """电动汽车的独特之处"""
    
    def __init__(self,make,model,year):
        """
     初始化父类的属性 
     再初始化电动汽车特有的属性 
     """ super().__init__(make,model,year)
     self.battery_size = 75                          #1

   def describe_battery(self):                         #2
    """打印一条描述电瓶容量的消息"""
    print(f"This car has a {self.battery_size}-kwh battery.")
my_tesla = ElectricCar('雅迪','model s ','2023') print(my_tesla.descriptive_name())
my_tesla.describe_battery()

 

让一个类继承另一个类后,就可以添加区分子类和父类所需的新属性和新方法了。

#1.添加了新属性self.battery_size,并设置其初始值。根据ElectricCar类创建的所有实例都将包含该属性。

#2.添加了新方法describe_battery()的方法,打印有关电瓶的信息