Decorators ,Lambda Functions and Sequences of Python

#using Decorators   not understand really
import operator
class Files:
    def __init__(self,**kwargs):
        self.properties =kwargs

    def copy(self):
        print "copying"
    def move(self):
        print "moving "
    def remove(self):
        print "deleting"
    def get_properites(self):
        return self.properties

    def get_property(self,key):
        return self.properties.get(key)

    @property
    def privacy(self):
        return self.properties.get("privacy")
    @privacy.setter
    def privacy(self,c):
        self.properties['privacy'] =c

    @privacy.deleter
    def privacy(self):
        del self.properties['privacy']



    #Lambda functions
    def calcuate(self,x):
        return x*2
    def cal2(self,j):
        t = lambda x: x * 2
        return t(j)

    # Multiple Arguments Using lambda
    def MulArg(self,x,y):
        t= lambda x,y:(x*y,x+y)
        return t(x,y)

    #Operators and KeyWords for sequences

    def OKs(self):
        t = tuple(range(29)) # tuple can not changed
        l= list(range(23))    # list can change
        print t
        print l

    # itemgetter Not easy really
    def itemgetee(self):
        getseconditem = operator.itemgetter(1)
        ls =['a','b','c','d','e']
        tu=('a','gg','d','c','f')
        print (getseconditem(ls))
        print (getseconditem(tu))
        print (operator.itemgetter(1,3,5)('abcdefg'))


def main():  # here not main(self)  ,will get error  TypeError: main() takes exactly 1 argument (0 given)
    imageDoc = Files(privacy="secret",you="your")
    print imageDoc.get_property("privacy")
    print imageDoc.get_property("you")

    D = Files()
    D.privacy="dddddddddd"
    print D.privacy # will get dddddddd
    print imageDoc.calcuate(3)  # works
    print D.calcuate(3)         # works
    print D.cal2(3)
    print D.MulArg(13,7)
    print D.MulArg(13, 7)[0]
    print D.MulArg(13, 7)[1]
    print D.OKs()
    D.itemgetee()

main()

转载于:https://www.cnblogs.com/TendToBigData/p/10501253.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值