最赞回答 / 慕函数7599421
假如<...code...>你可以推算def gcd(a, b)的过程(类似于for循环),得到两个数的最大公约数为3,这个3会在最终的结果中作为分母被除掉。没有没有def gcd(a, b)函数,那么我们得到的最终结果会有3/6,4/8这样的结果。
2022-01-24
def f(x):
return x.title
for item in map(f, ['alice', 'BOB', 'CanDY']):
print(item)
return x.title
for item in map(f, ['alice', 'BOB', 'CanDY']):
print(item)
2022-01-10
最赞回答 / 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