检测 [**] 是否是类的 [实例|子类]
isinstance(t, son) 检测 t 是否是 son 的实例
issubclass(son, Earth) 检测 son 是否是 Earth 的子类
class Earth:
pass
class son(Earth):
pass
t = son()
print(isinstance(t, son)) // True
print(issubclass(son, Earth)) // True
便捷编写方式
nums = [x for x in range(10)]
print(nums) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
类实例化后都会建立一个各自的字典,公共的属性调用类的
class Foo:
def __init__(self, name ,age):
self.name = name
self.age = age
x = 1
f1 = Foo('libai', 20)
f2 = Foo('baicai', 12)
print(id(f1.name) == id(f2.name)) # False
print(id(f1.age) == id(f2.age)) # False
print(id(f1.x) == id(f2.x)) # True f1.x 和 f2.x 指向的都是Foo的x
pop() 和remove的区别
l = ['1','2','3','4','5']
print(l)
print(l.pop()) 默认删除列表中的最后一个值,并取出
print(l)
print(l.pop(0)) 按指定索引的值去删除
print(l)
print(l.remove('4')) 按指定的值删除,返回值为None
print(l)
输出结果:
['1', '2', '3', '4', '5']
5
['1', '2', '3', '4']
1
['2', '3', '4']
None
['2', '3']
关于for,is 和 ==
看到一个小程序,很容易犯错的地方,pycharm 都提示出错了,但是最后是没问题的
i = 0 #这句不管存不存在,代码执行结果都是一样的,想想为什么
for i in range(10):
print(i)
print('\nend , i = ', i)
0123456789
end , i = 9 最后的i=9
is 方法是检测 id 是否相等,也就是内存地址是否相等,== 表示的是检测两个对象的值是否相等
参考:Python面试之 is 和 == 的区别
在 python beautifulsoup 中,如果存在
class="item item-cover current"
class="item item-cover"
可以使用 url_lists = bsobject.findAll('div',{"class":"item-cover"}) 来全部匹配