Python中“*”和“**”的用法 || yield的用法 || ‘$in’和'$nin' || python @property的含义

一、单星号 *

采用 * 可将列表或元祖中的元素直接取出,作为随机数的上下限:

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 }

更多mongodb查找(大于、小于......)

 五、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使方法像属性一样调用,就像是一种特殊的属性)

原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/12149842.html