Object-Oriented Programming
turple
A tuple
is a sequence of values. Unlike a list
, a tuple
can’t be modified.
can't be modified: tuple object does not support item assignment不能赋值
Like x, y, z
def main():
name, house = get_student()
print(f"{name} from {house}")
def get_student():
name = input("Name: ")
house = input("House: ")
return name, house
if __name__ == "__main__":
main()
line 7 return a turple with 2 elements,返回的是一个值
turple & index:
def main():
student = get_student()
print(f"{student[0]} from {student[1]}")
def get_student():
name = input("Name: ")
house = input("House: ")
return (name, house)
list
def main():
student = get_student()
if student[0] == "Padma":
student[1] = "Ravenclaw" #可修改
print(f"{student[0]} from {student[1]}")
def get_student():
name = input("Name: ")
house = input("House: ")
return [name, house]
dict
def main():
student = get_student()
if student["name"] == "Padma":
student["house"] = "Ravenclaw"
print(f"{student['name']} from {student['house']}")
def get_student():
student = {}
student["name"] = input("Name: ")
student["house"] = input("House: ")
return student
line 5 的 'name'
必须用单引号括,因为 print(f" ")
line 7 ~10 :
name = input("Name: ")
house = input("House: ")
return {"name": name, "house": house}
classes
Classes are a way by which, in object-oriented programming, we can create our own type of data and give them names.
python’s documentation of classes
class is like bluprint of a house, object is like a house
create new data type
class Student:
... # i will come back to implementa this later
create objects from classes
def get_student():
student = Student() # create objects/instance
student.name = input("Name: ") # . inside the student
student.house = input("House: ")
return student
.
access attributes of this variable student
of class Student
standardize
lay some groundwork for the attributes that are expected inside an object whose class is Student
define a class == get a function: eg. Student可以当作函数
def get_student():
name = input("Name: ")
house = input("House: ")
student = Student(name, house)
return student
methods
classes come with certain methods/functions inside of them and we can define
_init_
initialize the contents of an object from a class, we define this method
class Student:
def __init__(self, name, house):
self.name = name
self.house = house
def main():
student = get_student()
print(f"{student.name} from {student.house}")
def get_student():
name = input("Name: ")
house = input("House: ")
student = Student(name, house) # construct call, will goto def __init__
return student
def __init__
: create a function within class Student
, called a “method”
self gives us access to the current object that was just created
self.name = name
: add variables to objects like add values to keys in dict
line 11 student = Student(name, house)
python will call _init_ for us, 在这一步就已经分配内存
_init_ 只会执行一次
_str_
a specific function by which you can print the attributes of an object
class Student:
def __init__(self, name, house):
if not name:
raise ValueError("Missing name")
if house not in ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"]:
raise ValueError("Invalid house")
self.name = name
self.house = house
def __str__(self):
return f"{self.name} from {self.house}"
def main():
student = get_student()
print(student)
our own methods
class Student:
def __init__(self, name, house, patronus):
if not name:
raise ValueError("Missing name")
if house not in ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"]:
raise ValueError("Invalid house")
self.name = name
self.house = house
self.patronus = patronus
def __str__(self):
return f"{self.name} from {self.house}"
def charm(self):
match self.patronus:
case "Stag":
return "
标签:__,name,house,self,Object,Programming,Oriented,student,def
From: https://www.cnblogs.com/chasetsai/p/18320065