Python中“*”和“**”的用法 || yield的用法 || ‘$in’和'$nin' || python @property的含义
2024-09-04 02:56:21
一、单星号 *
采用 * 可将列表或元祖中的元素直接取出,作为随机数的上下限:
import random a = [1,4]
print(random.randrange(*a))
或者for循环输出:
import random a = [1,4]
for i in range(*a):
print(i)
'''
result :
1
2
3
'''
二、双星号 **
双星号 ** 可将字典里的“值”取出,如下例
class Proxy(object): def __init__(self,ip,port,protocol=-1,nick_type=-1,speed=-1,area=None,score=0,disable_ip=[]):
self.ip=ip
self.port=port
self.protocol=protocol
self.nick_type=nick_type
self.speed=speed
self.area=area
self.score=score
self.disable_ip=disable_ip a = {
'ip':'123',
'port':'9999',
'protocol':2,
'nick_type':1,
'speed':3,
'area':'jd.com',
'score':20,
'disable_ip':None
}
proxy = Proxy(**a)
print(proxy.__dict__)
__dict__ : 类的属性(包含一个字典,由类的数据属性组成),简单地说就是把数据转化成一个字典形式。
输出结果:
{'ip': '123', 'port': '9999', 'protocol': 2, 'nick_type': 1, 'speed': 3, 'area': 'jd.com', 'score': 20, 'disable_ip': None}
三、yield的用法
https://blog.csdn.net/mieleizhi0522/article/details/82142856
这里再补充一点就是生成器函数:
代码:
1 class People(object):
2
3 def __init__(self,age,name='嘿嘿'):
4 self.age=age
5 self.name=name
6
7 def dispaly():
8 for i in range(5):
9 peopel = People(i)
10 yield peopel
11
12 if __name__ == '__main__':
13 print(dispaly())
14 for peo in dispaly():
15 print(peo.__dict__)
控制台输出:
<generator object dispaly at 0x0000021E36B4A930>
{'age': 0, 'name': '嘿嘿'}
{'age': 1, 'name': '嘿嘿'}
{'age': 2, 'name': '嘿嘿'}
{'age': 3, 'name': '嘿嘿'}
{'age': 4, 'name': '嘿嘿'}
可以说,由yield的函数,相当于把所有函数返回值都放到一个容器里面。你可以通过遍历它来获取里面内容
这个时候的yield就和return不一样了,因为如果是return的话根本就不会输出age>0的对象
1 class People(object):
2
3 def __init__(self,age,name='嘿嘿'):
4 self.age=age
5 self.name=name
6
7 def dispaly():
8 for i in range(5):
9 peopel = People(i)
10 yield peopel
11
12 if __name__ == '__main__':
13 g=dispaly()
14 print(next(g).__dict__)
15 print(next(g).__dict__)
16 print(next(g).__dict__)
17
18 '''控制台输出
19 {'age': 0, 'name': '嘿嘿'}
20 {'age': 1, 'name': '嘿嘿'}
21 {'age': 2, 'name': '嘿嘿'}
22 '''
带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。yield在python 里就是一个生成器。当你使用一个yield的时候,对应的函数就是一个生成器了。生成器的功能就是在yield的区域进行迭代处理。
打印0~10000个数字: 1、生成一个列表n,再循环打印1-10000个数字,这样做会占用系统的内存; n = [i in [i in rang(0, 10000)] for i in n: print(i) 2、用下列生成器,就不用先生成列表,利用循环,每调用一次,就使用一次,不占内存空间 def gen(max): n=0 while n<=max: n+=1 yield n g = gen(10000) 就像使用迭代器一样,使用返回值 for i in g: print(i) print(next(g)) print(next(g)) print(next(g))
四、‘$in’和'$nin'
这两个东西是mongodb里面查询条件
看意思就知道‘$in’就代表找出在哪个范围内。‘$in’就代表找出不在哪个范围内的。
> db.tianyc02.find()
{ "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
{ "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
{ "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }
> db.tianyc02.find({age:{$in:[11,22]}})
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
{ "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }
> db.tianyc02.find({age:{$nin:[11,22]}})
{ "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
{ "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }
五、python @property的含义
@property类似于java中的private
class recet(object):
def __init__(self): self.width = 20 @property
def set_width(self,a=0):
self.width=a def get_width(self):
return self.width a = recet()
a.width=10
'''
a.set_width(50) #'NoneType' object is not callable
不能调用这个方法,会报错
'''
print(a.get_width())
(@property使方法像属性一样调用,就像是一种特殊的属性)
最新文章
- 03.LoT.UI 前后台通用框架分解系列之——多样的表格
- 获取微软原版“Windows 10 推送器(GWX)” 卸载工具
- [BCB] C++ BUILDER 绘图 随机生成图形
- js的解析--预处理(三)
- ps -ef能列出进程号,和端口号没任何关系
- 【转】 xcode中常用快捷键图文并茂解释
- AMQ学习笔记 - 19. 问题解决 - 控制Atomikos的日志输出
- Java学习笔记之:Java String类
- iOS开发——屏幕尺寸适配
- Plugin &#39;FEDERATED&#39; is disabled 或 1067错误 启动错误与“服务 mysql 意外停止”解决方法
- Linux系统最小化安装之后的系统基础环境安装以及内核优化脚本
- 【Java】 实现一个简单文件浏览器(2)
- selenium-判断元素是否可见(五)
- wget无法正确下载jdk解决方案
- Factors of Factorial AtCoder - 2286 (N的阶乘的因子个数)(数论)
- python IO 多路复用
- Mybatis复杂嵌套关联一例
- 洛谷P2054 [AHOI2005]洗牌(扩展欧几里德)
- SAP ERP 与SAP CRM有什么不同?
- C语言简明数据类型指南
热门文章
- 【EXP】根据字段导出数据query
- ctfhub技能树—RCE—过滤空格
- buuctf—web—Easy Calc
- 关于postgresql中numeric和decimal的精度和标度问题
- Pytorch入门——手把手教你MNIST手写数字识别
- [usaco2010 Oct]Soda Machine
- java 日期与时间操作
- 【Android】报错 Please ensure Hyper-V is disabled in Windows Features, or refer to the Intel HAXM 的解决方案
- uni-app开发经验分享五: 解决三端页面兼容问题的方法
- JavaScript中函数的调用!