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

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

猜你喜欢

转载自www.cnblogs.com/kongbursi-2292702937/p/12149842.html
今日推荐