c语言中presentvalue函数,python随用随学20200118-函数的高级特性

高阶函数

话说当年C语言和Java里好像都有这么个东西...忘了

一句话说就是函数名本身就是一个引用. 可以作为变量传递.

一个简单的例子:

def power_demo(x):

return x*x

def foo(num1,num2,fun):

return fun(num1) + fun(num2)

print(foo(2,3,power_demo))

map/reduce的用法

map()方法

map(function, iterable, ...)

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().

解读:

入参是一个函数和一个可迭代对象. 函数依次作用于可迭代对象上.

返回值是一个迭代器

def f(x):

return x*x

L = map(f,[1,2,3,4,5,6])

print(type(L))

print(list(L))

reduce()方法

functools.reduce(function, iterable[, initializer])

Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5).

The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.

文档中给出的大概的实现

def reduce(function, iterable, initializer=None):

it = iter(iterable)

if initializer is None:

value = next(it)

else:

value = initializer

for element in it:

value = function(value, element)

return value

一个例子:

from functools import reduce

def foo(x,y):

return x*10+y

result = reduce(foo,[1,3,5,7,9])

print(type(result),result)

filter()方法

filter(function, iterable)

Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.

先来个简单的例子,筛选奇数

def is_odd(x):

return x%2 ==1

print(list(filter(is_odd,[1,2,3,4,5,6,7,8,9])))

举个例子,比如使用传说中的埃拉托色尼筛选法筛选出所有素数

def odd_iter():

n = 1

while True:

n = n + 2

yield n

def not_divisible(n):

return lambda x: x % n > 0

def primes():

yield 2

it = odd_iter()

while True:

n = next(it)

yield n

it = filter(not_divisible(n), it)

sorted()函数

自定义排序的一个函数. 举个简单的例子吧,感觉一是半会儿用不上.

sorted([36, 5, -12, 9, -21], key=abs,reverse=True)

python中的闭包

变量的作用域

不写了,回头发文章补齐

嵌套函数

函数里套函数,发文章的时候补齐

什么是闭包

看维基百科的学术定义

在计算机科学中,闭包(Closure)是词法闭包(Lexical Closure)的简称,是引用了自由变量的函数。这个被引用的自由变量将和这个函数一同存在,即使已经离开了创造它的环境也不例外。所以,有另一种说法认为闭包是由函数和与其相关的引用环境组合而成的实体。

说啥呢看不懂...

举个例子:

def print_msg():

msg = "hello world"

def printer():

print(msg)

# 注意,这里没带括号,返回的是printer()这个函数的引用(指针)

return printer

# 这里等于通过print_msg()拿到了内部函数printer()的引用,所以printer这个变量指向了一个叫printer的函数

printer = print_msg()

print(printer)

printer()

pic-1582723050329.png

pic-1582723050329.png

不是所有嵌套函数都是闭包,就如同不是所有牛奶都是...跑题了

看箭头的位置,按照剧本,当函数执行完成后,其内部的局部变量应该已经销毁.

也就是说在print_msg()这函数执行完了之后,msg这个变量应该不存在了.

但是,BUT!!!

当我们通过print_msg()返回的引用去执行其内部的函数printer()的时候,msg这个变量又神奇的被打印出来了.

这里的printer变量就是一个闭包.

以下是我个人的理解,如果有错误,烦请指出. 因为python的GC使用的是引用计数机制.

所以当我们执行printer = print_msg()的时候,虽然print_msg()函数执行完了,但是在执行过程中,创建了一个字符串对象'hello world' (并将其引用赋值给了msg)和一个函数对象printer. 然后printer中又保留了到字符串对象msg的引用.所以在执行上述语句的时候发生了这么一个关系printer(外面的变量,这个名字起瞎了)-->printer--->msg

所以在函数print_msg执行完毕之后,变量msg的引用并没有变成0,这个变量依旧存在,没有被GC处理掉.

闭包有啥用

闭包避免了使用全局变量. 闭包允许函数将与其操作的某些数据(环境))关联起来.

在面向对象的编程中,对象允许我们将一些数据(属性)和一个或多个方法关联起来.

但是当对象中只有一个方法的时候,使用闭包或许是更好的选择.

def foo(x):

def foo_inner(y):

return x+y

return foo_inner

foo1 = foo(5)

print(foo1(10))

print(foo1(6))

pic-1582723050329.png

pic-1582723050329.png

匿名函数

就是lambda表达式. lambda 入参 : 返回值

冒号前面是入参,冒号后面是返回值.用来简写函数的. 比如

L = list(map(lambda x:x*x,[1,2,3,4,5]))

print(L)

不好理解,新手别瞎JB用

装饰器

面向对象的语言中,万物都是对象,方法(函数)当然也是一个对象. 是对象就要有属性方法和引用. 函数对象有一个__name__的属性可以拿到函数的名称.

def now():

print("2020-02-18")

foo = now

print(foo.__name__)

print(now.__name__)

这俩输出肯定是一样的. 不懂的话C语言基础(非谭浩强版)从头学

那么如果现在我们想增强now()方法的功能,除了重写这个方法之外还有没有其他的办法?

想想嵌套函数貌似是可以的.

def log(func):

def wrapper(*args, **kw):

#这里是我们想增强的代码

print('this is a log: function "%s" is called'%func.__name__)

return func(*args, **kw)

return wrapper

@log

def now():

print("2020-02-18")

这玩意儿就有点装饰器的样子了.

在now函数上面写个@log实际上就是在执行 log(now())

但是有个问题.

pic-1582723050330.png

改造后的代码就是这个样子的.

import functools

def log(func):

@functools.wraps(func)

def wrapper(*args, **kw):

#这里是我们想增强的代码

print('this is a log: function "%s" is called'%func.__name__)

return func(*args, **kw)

return wrapper

@log

def now():

print("2020-02-18")

print(now.__name__)

这里我们又发现一个神器的东西. 就是装饰器functools.wraps带参数...这玩意儿咋实现的呢?

看个例子:

def log(text):

def decoraotor(func):

@functools.wraps(func)

def wrapper(*args, **kwargs):

print("%s %s" % (text, func.__name__))

return func(*args, **kwargs)

return wrapper

return decoraotor

@log("Holy shit!")

def now():

print("2020-02-18")

真特么复杂...用到的时候翻笔记吧. 记不住记不住

偏函数

python中的偏函数不是数学上的偏函数(话说数学上的偏函数是啥,我也不知道...),看文档定义:

functools.partial(func, /, *args, **keywords)

Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords. If more arguments are supplied to the call, they are appended to args. If additional keyword arguments are supplied, they extend and override keywords. Roughly equivalent to:

def partial(func, /, *args, **keywords):

def newfunc(*fargs, **fkeywords):

newkeywords = {**keywords, **fkeywords}

return func(*args, *fargs, **newkeywords)

newfunc.func = func

newfunc.args = args

newfunc.keywords = keywords

return newfunc

The partial() is used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature. For example, partial() can be used to create a callable that behaves like the int() function where the base argument defaults to two:

from functools import partial

basetwo = partial(int, base=2)

basetwo.__doc__ = 'Convert base 2 string to an int.'

basetwo('10010')

partial Objects

partial objects are callable objects created by partial(). They have three read-only attributes:

partial.func

A callable object or function. Calls to the partial object will be forwarded to func with new arguments and keywords.

partial.args

The leftmost positional arguments that will be prepended to the positional arguments provided to a partial object call.

partial.keywords

The keyword arguments that will be supplied when the partial object is called.

partial objects are like function objects in that they are callable, weak referencable, and can have attributes. There are some important differences. For instance, the__name__ and __doc__attributes are not created automatically. Also, partial objects defined in classes behave like static methods and do not transform into bound methods during instance attribute look-up.

看一个例子就得了.高级玩法,新手别瞎JB玩.

def int2(x,base=2):

return int(x,base)

print(int2('10000000'))

int22 = functools.partial(int,base=2)

print(int22('10000000'))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot并没有原生支持BACnet协议,但是可以通过一些第三方库来实现BACnet协议的集成。其一个比较常用的库是BACnet4J,它是一个Java实现的BACnet协议栈。你可以在Spring Boot应用引入BACnet4J库,并使用它提供的API来实现BACnet协议的通信。下面是BACnet4J库的示例代码: ```java BACnetStack stack = new BACnetStack(new LocalDevice(1234, new DefaultTransport(new IpNetworkTransport(new InetSocketAddress("192.168.1.1", 47808))))); stack.initialize(); // 发送读操作 ObjectIdentifier objectId = new ObjectIdentifier(ObjectType.analogInput, 1); ReadPropertyRequest request = new ReadPropertyRequest(objectId, PropertyIdentifier.presentValue); Response response = stack.send(request, new IpNetworkAddress("192.168.1.2", 47808)); if (response instanceof ReadPropertyAck) { ReadPropertyAck ack = (ReadPropertyAck) response; Object value = ack.getValue(); System.out.println("读操作成功,结果为:" + value); } else { System.out.println("读操作失败,错误码为:" + response.getException()); } // 发送写操作 WritePropertyRequest request = new WritePropertyRequest(objectId, PropertyIdentifier.presentValue, new Real(20.0f)); Response response = stack.send(request, new IpNetworkAddress("192.168.1.2", 47808)); if (response instanceof WritePropertyAck) { System.out.println("写操作成功"); } else { System.out.println("写操作失败,错误码为:" + response.getException()); } stack.terminate(); ``` 这段代码演示了如何使用BACnet4J库实现BACnet协议的读写操作。需要注意的是,这里使用了IP网络传输,你需要将代码的IP地址和端口号替换成你实际使用的BACnet设备的IP地址和端口号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值