我也用的instance,也想知道为什么不行😀
PS D:\python> python -m http.server --directory c:/
# Enter a code
# coding=utf-8
class Animal(object):
pass
dog = Animal()
dog.name = '汪汪'
dog.age = 7
# 三种方式都可以,最后一种3.6新功能,在网页上还不支持,可以本地执行
print("%s : %s" % (dog.name, dog.age))
print("{} : {}".format(dog.name, dog.age))
print(f"{dog.name}: {dog.age}")
cat = Animal()
cat.name = '喵喵'
cat.age = 3
print("%s : %s" % (cat.name, cat.age))
print("{} : {}".format(cat.name, cat.age))
print(f"{cat.name}: {cat.age}")
需要忽略证书校验
response = requests.get('https://www.imooc.com', verify=False)
当Python子类继承父类时,通常不需要重新列出所有的参数,只需要在子类的初始化方法中调用父类的初始化方法即可。这样可以确保子类继承了父类的属性和方法。
对于多重继承和祖先类的情况,子类需要在初始化方法中分别调用每个父类的初始化方法,以确保所有祖先类的属性和方法都被正确继承。在Python中,可以使用super()
函数来实现这一点,它可以帮助子类调用父类的方法。
以下是一个示例代码,展示了Python中子类继承父类以及多重继承时如何处理参数的情况:
class Parent1: def __init__(self, param1): self.param1 = param1 class Parent2: def __init__(self, param1, param2): self.param1 = param1 self.param2 = param2 class Child(Parent1, Parent2): def __init__(self, param1, param2, param3): super().__init__(param1) # 调用第一个父类的初始化方法 super(Parent1, self).__init__(param2) # 调用第二个父类的初始化方法 self.param3 = param3 child_obj = Child('A', 'B', 'C') print(child_obj.param1) # 输出:B print(child_obj.param2) # 输出:B print(child_obj.param3) # 输出:C
sqrt 返回的是float类型,用type int去判断,可以无法返回值
import math
def f(n):
r = int(math.sqrt(n))
return r * r == n
print([item for item in filter(f, range(1, 101))])
在继承了两个方法的情况下,super()只会调用第一个函数的__init__ 方法,而不会调用第二个函数,所以会出现传参多的问题。修改一下代码,例如:
# Enter a code
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Student(Person):
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score
class Teacher(Person):
def __init__(self, name, gender, score):
super(Teacher, self).__init__(name, gender)
self.score = score
class SkillMixin(object):
def __init__(self, sports):
self.sports = sports
class BasketbaMixin(SkillMixin):
def __init__(self, sports):
super(BasketbaMixin, self).__init__(sports)
class FootballMixin(SkillMixin):
def __init__(self, sports):
super(FootballMixin, self).__init__(sports)
class PlayBasketballStudent(BasketbaMixin, Student):
def __init__(self, name, gender, score, sports):
Student.__init__(self, name, gender, score)
BasketbaMixin.__init__(self, sports)
def get_info(self):
return 'I am a Student, my name is %s, I am a %s, I like %s, my score is %s' % (self.name, self.gender, self.sports, self.score)
class PlayFootballTeacher(Teacher, FootballMixin):
def __init__(self, name, gender, score, sports):
Teacher.__init__(self, name, gender, score)
FootballMixin.__init__(self, sports)
def get_info(self):
return 'I am a Teacher, my name is %s, my score is %s, I like %s' % (self.name, self.score, self.sports)
student = PlayBasketballStudent("Alice", "female", "90", "basketball")
teacher = PlayFootballTeacher("Bob", "male", "30", "football")
print(teacher.get_info())
print(student.get_info())
代码第一行指定:
# -*- coding: utf-8 -*-
pycharm环境配置问题
还真不继承
不可以
提示沒有定義一般是把把該值當一個變量或者對象在使用,這個問題需要你提供實際代碼了
在Python中,pass只是一個佔位符,沒任何實際作用
没有读取文件内容
第二行之后加上
print(f.readlines()) # 读取并输出f中的所有内容
f.close() #关闭文件
7/10
3/10
1/10
5/2
[Done] exited with code=0 in 0.069 seconds
没问题啊,可以正常运行,你复制的时候漏了啥吧
a+模式只能从末尾追加,write操作不受seek影响
如果有重复的,会输出先继承的父类,也就是继承括号中的前者。如果属性相同,则需要对父类对象中内建函数,通过调用函数的方式,输出继承父类的属性
# 关于你这个情况,我尝试了两种理解方法,我自己称为显示调用和继承式调用,具体代码如下:
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Student(Person):
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score
class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher, self).__init__(name, gender)
self.course = course
class Skill(object):
def __init__(self, sport):
self.sport = sport
class PlayBasketball(Skill):
def __init__(self, sport):
super(PlayBasketball, self).__init__(sport)
class PlayFootball(Skill):
def __init__(self, sport):
super(PlayFootball, self).__init__(sport)
# 显示地调用父类的init初始化方法
class StudentBasketball(PlayBasketball, Student):
def __init__(self, name, gender, score, sport):
Student.__init__(self, name, gender, score)
PlayBasketball.__init__(self, sport)
def can_play(self):
return 'I am %s ,i am %s, i got score %s ,i can play%s' % (self.name, self.gender, self.score, self.sport)
# 显示地调用父类的init初始化方法
class TeacherFootball(PlayFootball, Teacher):
def __init__(self, name, gender, course, sport):
Teacher.__init__(self, name, gender, course)
PlayFootball.__init__(self, sport)
def can_play(self):
return 'I am %s ,i am %s,i teach %s ,i can play%s' % (self.name, self.gender, self.course, self.sport)
# 继承式地调用父类的init初始化方法
class StudentPlayBasketball(PlayBasketball, Student):
def __init__(self, name, gender, score, sport):
super(StudentPlayBasketball, self).__init__(name, gender, score)
super(StudentPlayBasketball, self).__init__(sport)
def can_play(self):
return 'I am %s ,i am %s, i got score %s ,i can play%s' % (self.name, self.gender, self.score, self.sport)
# 继承式地调用父类的init初始化方法
class TeacherPlayFootball(PlayFootball, Teacher):
def __init__(self, name, gender, course, sport):
super(TeacherPlayFootball, self).__init__(name, gender, course)
super(TeacherPlayFootball, self).__init__(sport)
def can_play(self):
return 'I am %s ,i am %s,i teach %s ,i can play%s' % (self.name, self.gender, self.course, self.sport)
student = StudentBasketball('Jason', 'Male', 98, 'Basketball')
teacher = TeacherFootball('Alice', 'Female', 'English', 'Football')
student1 = StudentBasketball('Jason', 'Male', 98, 'Basketball')
teacher2 = TeacherFootball('Alice', 'Female', 'English', 'Football')
print(student1.can_play())
print(teacher2.can_play())
前两行是对象在内存中的地址,每个Python对象都有一个唯一的内存地址,用于标识该对象在内存中的位置。通常情况下,我们不需要直接操作对象的内存地址,而是通过对象的属性和方法来访问和修改其值。
后面就布尔类型值,表示不相等
看下文件开头有加:# coding:utf-8
没问题,就是对三个私有属性封装了3个get方法
dog.instance_count
=
dog.instance_count
+
1 实际上不是在操作类属性 而是在dog实例上新增了一个 instance_count属性,然后你再次访问的时候因为优先级 你访问不到类上的属性了 所以操作类属性最好使用Animal.instance_count
需要给变量重新赋值
num = str(num)
print
(
'I hava a %s skill'
%
self
.skill)都改成return
'I hava a %s skill'
%
self
.skill。
或者
print
(a.get_skill())改成a.get_skill()直接运行,因为你定义的
def
get_skill(
self
):自带打印了。
s, addr = server.accept()
server.accept() 返回的是一个元组Tuple,具体如下
(<socket.socket fd=332, family=2, type=1, proto=0, laddr=('127.0.0.1', 8099), raddr=('127.0.0.1', 5726)>, ('127.0.0.1', 5726))
s,addr 分别介绍元组的第一个和第二个元素:
s = <socket.socket fd=332, family=2, type=1, proto=0, laddr=('127.0.0.1', 8099), raddr=('127.0.0.1', 5726)>
addr = ('127.0.0.1', 5726)
都是可被调用的
r1 = Rational(1, 2)
这里是调用了Rational类的__init__方法创建了一个实例,然后将这个实例赋予给了r1;
self.p * r.q + self.q * r.p, self.q * r.q
这是调用了r1这个实例的__add__方法,self是指实例本身也就是r1,而r这个参数就是指代r2,下文两个语句是等价的
print(r1+r2) print(r1.__add__(r2))
从这地方引申出来,假设还有一个r3,那程序是怎么跑的呢
r1 = Rational(1, 2) r2 = Rational(2, 3) r3 = Rational(3, 4) print(r1+r2+r3)
程序会先算r1+r2,然后再用这个算出的结果x去+r3
看看环境有没有配置好
类自己的name 被赋值为 实例生成时候传输的name
在你的当前工程目录下建立一个文本文件。这样读取文件时,不用写路径