class A: pass class B: pass class D(B,A): pass class C(A,B): pass class E(C,D): pass
问题:为何这个多重继承会报错呢?
已经明白了,老师,不用回了,谢谢老师,一个小bug我没发现到,少了个括号
class ComputerFactory: __obj = None __init_flag = True __countLenovo = 0 __countAsus = 0 __countHasee = 0 def __new__(cls, *args, **kwargs): #单例模式 if cls.__obj==None: cls.__obj = object.__new__(cls) return cls.__obj #new方法创建完成一定要返回我们的实例对象 def __init__(self): #单例模式 if ComputerFactory.__init_flag: print('构造初始化') ComputerFactory.__init_flag = False def produce(self,brand): #工厂模式 if brand =='联想': ComputerFactory.__countLenovo += 1 return Lenovo() elif brand == '华硕': ComputerFactory.__countAsus += 1 return Asus() elif brand == '神舟': ComputerFactory.__countHasee += 1 return Hasee() def calculate(self): a = [ComputerFactory.__countAsus,ComputerFactory.__countLenovo,ComputerFactory.__countHasee] print('本公司一共生产了{0}台电脑'.format(sum(a)) class Computer: __count = 0 def __init__(self): Computer.__count +=1 def calculate(self): print('生产了{0}台电脑'.format(Computer.__count)) class Lenovo(Computer): __num = 0 def __init__(self): # Computer.__init__(self) Lenovo.__num +=1 def calculate(self): print('共生产了{0}个联想电脑'.format(Lenovo.__num-1)) class Asus(Computer): __num = 0 def __init__(self): # Computer.__init__(self) Asus.__num += 1 def calculate(self): print('共生产了{0}个联想电脑'.format(Asus.__num-1)) class Hasee(Computer): __num = 0 def __init__(self): # Computer.__init__(self) Hasee.__num += 1 def calculate(self): print('共生产了{0}个联想电脑'.format(Hasee.__num-1)) t = ComputerFactory() h = Computer() t.produce('联想') h.calculate() Lenovo().calculate() t.calculate() Lenovo().calculate()
问题:不知道为啥会报这样的错,我反复核实没有格式的错误啊,求指导!老师
必须加grade储存吗?不加那个grade=“”会报错吗?
class Student: def __init__(self,name): self.name = name def __str__(self): return '名字是:',self.name p = Student('黄伟') print(p)
问题:为啥用,self.name就报错,而用字符格式化就可以?
class Person: def work(self): print('你处于在职状态中') def play_game(self): print('{0}在打游戏'.format(self)) def work2(s): print('好好工作,努力赚钱,迎娶媳妇{0}'.format(s)) p = Person() Person.play =play_game p.play() Person.work = work2 p.work()
老师,这里的动态给类增加新的方法,可以理解为该方法是该类属性的方法,从属于play和work属性的。
请老师看看我的问题,答疑一下,在功能上可以实现,总觉得不是完善的,求指导!谢谢!
问题:是不是没有这种用法,强制命名规则只能用在带一个星号的形参后面?
class Student: def __init__(self): print('这是构造函数') def __del__(self): print('销毁对象:{0}'.format(self)) p1 =Student() print(p1) P2 =Student() p3 =Student() del p3 print('程序结束')
问题:当程序结束后,Python解释器触发析构函数,p1和p2哪个最先被删除,他们的删除顺序是怎么确定的?
#使用 1 个函数代替上面的两个函数 def printName(isChinese,name,familyName): def inner_print(a,b): print("{0} {1}".format(a,b)) if isChinese: inner_print(familyName,name) else: inner_print(name,familyName) printName(True,"小七","高") printName(False,"George","Bush") 老师为啥打印printName(True,"小七","高")会是高小七
问题:定义一个用户名admin,密码123,写一个登录程序,每次输入错误时,给予提示,还有几次机会,如果用户名或者密码输入错误超过三次,则锁定用户(不允许再输入用户名密码了)
代码如下:
countNum = 1 admin = 'huangwei' password = 123 for i in range(4): infor_admin = input('请输入用户名(还有{0}次机会):'.format(4-countNum)) infor_password = input('请输入密码(还有{0}次机会):'.format(4-countNum)) if countNum == 3: print('账号已锁定,请联系管理员!') break if int(infor_password) ==password and infor_admin == admin: print('密码正确,欢迎换来!') break else: print('密码不正确!') countNum +=1
请老师帮我看一下,我这个代码能实现上面的功能吗?总感觉自己做的略简单了,没有那种登录界面的效果
def f1(**a,b,c): print(a,b,c) #打印的时候要直接打印a不带星号 f1(name ='safa',b=12,c=21)
如果形参中是双星号的话,后面也一样采用强制命名,但是输出就报错,老师,是不是没有这种用法,强制命名原则只能用在带一个星号的可变参数后面呢???
x*2 for x in range(5) 那个in表示什么意思?表示属于range(5)的意思吗?
有个疑问,为啥我两个打印出来的东西不一样呢?
class Solution: def __init__(self): self.c = [] def twoSum(self,nums,target): i = 0 j=len(nums)-1 num = sorted(nums) while j>i: if num[i] + num[j] ==target: self.c.append([i,j]) j -=1 elif num[i] +num[j] < target: i += 1 else: j -=1 return self.c a1 = Solution().twoSum([1,2,4,5,6],7) print(a1) print(Solution().c)
Solution: (): .c = [] (numstarget): i = j=(nums)-num = (nums) j>i: num[i] + num[j] ==target: .c.append((ij)) j -=num[i] +num[j] < target: i += : j -=Solution().twoSum([]) (Solution().c)
老师我想问问,为啥这个地方我的c一直是空的,我想的话输出[(0,4),(1,3)],搞晕了
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637