- 如何在dict中add/append键值对dict.update()
# Dictionary of strings and ints
wordFreqDic = {
"Hello": 56,
"at" : 23 ,
"test" : 43,
"this" : 43
}
# Adding a new key value pair
wordFreqDic.update( {'before' : 23} )
#结果
test : 43
this : 43
Hello : 56
at : 23
before : 23
- @classmethod修饰符
classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。如下:
class A(object):
a = 1
def def1(self):
print("foo")
@classmethod
def def2(cls):
print('def2')
print(cls.a)
cls().def1() # 调用 foo 方法
A.def2() #不需要实例化,直接调用def2