这里是py2的环境,Person父类首先要继承object类,然后Student类的super方法也要传继承的子类和self两个参数才能调用__init__方法,py3的话父类不需要继承object类,spuer方法也不用传参就能直接调用
2022-05-09
class Person(object):
def __init__(self, name,age,location):
self.__name = name
self.__age=age
self.__location=location
def get(self):
return '{},{},{}'.format(self.__name,self.__age,self.__location)
p = Person('Alice',22,'hongkong')
print(p.get())
def __init__(self, name,age,location):
self.__name = name
self.__age=age
self.__location=location
def get(self):
return '{},{},{}'.format(self.__name,self.__age,self.__location)
p = Person('Alice',22,'hongkong')
print(p.get())
2022-04-23
更习惯这种写法:
def is_odd(x):
return x % 2 == 1
item =filter(is_odd, [1, 4, 6, 7, 9, 12, 17])
print(item)
def is_odd(x):
return x % 2 == 1
item =filter(is_odd, [1, 4, 6, 7, 9, 12, 17])
print(item)
2022-04-17
def format_name(s):
return s.title()
item = map(format_name, ['alice', 'BOB', 'CanDY'])
print(item)
这样也行,更容易理解。很奇怪,答案中为什么没有报错,还是不理解python运行的内部机制
return s.title()
item = map(format_name, ['alice', 'BOB', 'CanDY'])
print(item)
这样也行,更容易理解。很奇怪,答案中为什么没有报错,还是不理解python运行的内部机制
2022-04-17
它这也是要实例化之后才能使用的,跟直接定义一个方法,然后访问p.calls()有何不同?实际使用场景是什么呢?
2022-03-18
后面的例子可以理解成:
调用count()时给fs这个list每个位置都赋值了一个 i*i的函数,但因为返回值是函数f本身所以并没有调用,即fs = [i*i, i*i, i*i],此时里面的i并不表示实际值;当count()被调用完后for循环里的i已经到3,所以当调用fs这个list内的函数f1/f2/f3时都计算i*i,此时i代入3,所以三个结果都为9。
调用count()时给fs这个list每个位置都赋值了一个 i*i的函数,但因为返回值是函数f本身所以并没有调用,即fs = [i*i, i*i, i*i],此时里面的i并不表示实际值;当count()被调用完后for循环里的i已经到3,所以当调用fs这个list内的函数f1/f2/f3时都计算i*i,此时i代入3,所以三个结果都为9。
2022-02-15