最赞回答 / weixin_慕设计1349666
class Animal(): def __init__(self,name,age,location): self.__name=name self.__age=age self.__location=location def get_info(self): return 'name={},\nage={},\nlocation={}'.format(self.__name,self.__age,self.__location) ...
2022-01-07
Traceback (most recent call last):
File "index.py", line 27, in
print(r1 / r2)
TypeError: unsupported operand type(s) for /: 'Rational' and 'Rational'
本节的代码运行错误,提示地板除的那一行报错,/没有定义,把__truediv__改成__div__运行正确
File "index.py", line 27, in
print(r1 / r2)
TypeError: unsupported operand type(s) for /: 'Rational' and 'Rational'
本节的代码运行错误,提示地板除的那一行报错,/没有定义,把__truediv__改成__div__运行正确
2022-01-06
运行下面一段就成功
class Animal: pass
dog = Animal()
cat = Animal()
dog.name = 'xiaohong'
dog.age = 13
cat.name = 'xiaohong2'
cat.age = 14
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
class Animal: pass
dog = Animal()
cat = Animal()
dog.name = 'xiaohong'
dog.age = 13
cat.name = 'xiaohong2'
cat.age = 14
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
2021-12-16
__call__魔法方法可以将类当作函数来使用,当作为函数来使用时,调用call方法。
例如 文中的 p("Alice"),即相当于之行p.__call__("Alice")
例如 文中的 p("Alice"),即相当于之行p.__call__("Alice")
2021-12-08
def f(x):
return x.title()
l = []
for item in map(f, ['alice', 'BOB', 'CanDY']):
l.append(item)
print(l)
return x.title()
l = []
for item in map(f, ['alice', 'BOB', 'CanDY']):
l.append(item)
print(l)
2021-12-01
最新回答 / hermaniu
Traceback (most recent call last): File "C:\Users\hermaniu\Desktop\test1.py", line 162, in <module> w=Work('Herman',96,'english',22) File "C:\Users\hermaniu\Desktop\test1.py", line 156, in __init__ super(Work,self).__init__(name,score,course...
2021-11-23
最赞回答 / 慕粉_pp
关于调用两种方法的时机使用print()时使用%s和f'{}'拼接对象时使用str(x)转换对象x时在上述三种场景中,会优先调用对象的__str__()方法;若没有,就调用__repr__()方法;若再没有,则显示其内存地址。特别地,对于下面两种场景:用%r进行字符串拼接时用repr(x)转换对象x时则会调用这个对象的__repr__()方法;若没有,则不再看其是否有__str__()方法,而是显示其内存地址。<...code...>
2021-11-20
最赞回答 / hermaniu
实例本身无count,get_count定义的是类方法,因此Leo.get_count()返回Animal的私有属性__count=0,set_count是实例方法对类无效,因此获取的__count 还是原本的0.
2021-11-18