面向对象编程

根据类来创建对象称为实例化。这里只过一下大概的面向对象的内容,不做细讲。可以直接查阅资料。https://www.runoob.com/python3/python3-class.html

创建和使用类及实例

给出一个类的使用例子:

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    def sit(self):
        printf(f"{self.name} is now sittig.")
        
  	def roll_over(self)
		print(f"{self.name} rolled over!")
        
my_dog = Dog("willie", 6)	#实例化一个对象

注意:__init__() 是一个特殊的方法,当类创建一个新的实例时,会自动运行该方法。类似于C++里的构造函数。而self是指向实例本身的引用。每个函数默认第一个参数都是self。每个与实例相关联的方法调用都自动传递实参self,它是一个指向实例本身的引用,让实例能够访问类中的属性和方法。以 self 为前缀的变量可供类中的所有方法使用,可以通过类的任何实例来访问。

继承

在既有类的基础上编写新类时,通常要调用父类方法__init()__。这将初始化在父类__init()__ 方法中定义的所有属性,从而让子类包含这些属性。给出一个例子:

动物基类代码:

class Animal:
    def __init__(self, ptype, sound, color):
        self.ptype = ptype
        self.sound = sound
        self.color = color
        
	def get_sound(self):
        print(f"这只{self.ptype}的叫声是{self.sound}")
    
    def get_color(self):
        print(f"这只{self.ptype}的颜色是{self.color}")
        

狗派生类实现代码:

class Dog(Animal): #子类继承父类,是需要将父类放在子类括号中
    def __init__(self, ptype, sound, color):
        #初始化父类属性
        super().__init__(ptype, sound, color) #特殊方法,可以在子类中调用父类方法
        
    def get_sound(self):
        print(f"这只{self.ptype}它竟然不叫")
        
my_tesla = Dog("狗", "汪汪汪", "黑色")
my_tesla.get_color()
my_tesla.get_sound()

对于父类的方法,只要它不符合子类模拟的实物的行为,都可以进行重写。为此,可在子类中定义一个与要重写的父类方法同名的方法。这样,Python将不会考虑这个父类方法,而只关注在子类中定义的相应的方法。

导入模块

在 python 用 import 或者 from...import 来导入相应的模块。

将整个模块(Allmodule)导入,格式为: import Allmodule

从某个模块中导入某个函数,格式为: from Allmodule import somefunction

从某个模块中导入多个函数,格式为: from Allmodule import firstfunc, secondfunc, thirdfunc

将某个模块中的全部函数导入,格式为: from Allmodule import *

使用别名导入函数,格式为: from Allmodule import firstfunc as Func

Python标准库

https://pymotw.com/3/

文件读取数据

读取整个文件

先给出一个例子,对这个例子进行解释分析。读取一个文件,内容是圆周率的后30位的数值,且在小数点后每10位处换行。

with open("pi_digits.txt") as file_object:
    contents = file_object.read()
print(contents)
#文件内容
/*
3.1415926535
  8979323846
  2643383279

*/
  • open() 在当前目录下查找指定文件。返回一个对象,用as对这个返回值起别名为 file_object
  • 关键字 with 在不再需要访问文件后将其文件对象进行关闭
  • 文件对象调用read()方法读取文件内容,当到达文件末尾时返回一个空字符串,而将这个空字符串显示出来就是一个空行

注意:可以调用 open()close() 来打开和关闭文件,但这样做时,如果程序存在bug导致方法 close() 未执行,文件将不会关闭。所以使用上面的结构读取文件时,Python会在合适的时候自动将其关闭。

删除多余的空行可以使用 rstrip() contents.rstrip()

对了open函数里既可以填写绝对路径和相对路径,这点就不说了,简单。不懂上网查。

逐行读取文件

filename = "pi_digits.txt"

with open(filename) as file_object:
    for line in file_object:
        print(line.rstrip())

注意:文件中每一行的末尾都是有一个换行符的。因此要对读取的每一行字符串内容进行删除多余的空白行,使用 rstrip()

创建一个包含文件各行内容的列表:
readlines() 从文件中读取每一行,并将其存储在一个列表中

filename = "pi_digits.txt"

with open(filename) as file_object:
    lines = file_object.readlines()

for line in lines:
    print(line.rstirp())

使用文件内容组成完整字符串:

filename = "pi_digits.txt"

with open(filename) as file_object:
    lines = file_object.readlines()

pi_string = ""
for line in lines:
    pi_string += line.rstrip()
    
print(pi_string)	# 结果为 3.1415926535 8979323846 2643383279
print(len(pi_string))	# 32

由于pi_string指向的字符串包含原来位于每行左边的空格,为删除这些空格,可使用 strip()
读取文本文件时,Python将其中的所有文本都解读为字符串。

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/BrokenSnow/p/17563607.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!