当前位置:网站首页>Closures and decorators
Closures and decorators
2022-07-19 07:01:00 【I'm Zha Zha Hui】
List of articles
Closure
What is a closure ?
A closure is an internal function defined in an external function , Internal functions refer to variables in external functions , The return value of the external function is the internal function
A closure is an entity composed of a function and its associated reference environment ( namely : Closure = function + Citation environment )
The characteristics of closures ?
Nested function
The inner function refers to the variable of the outer function
Take the inner function as the return value of the outer function
Define closure , The completed functions are : Pass in a number, sum it and output
for example : Pass in 10 -> Output 10
Pass in 15 -> Output 25
Pass in 20 -> Output 45
def outer():
sum1 = 0
def inner(num):
nonlocal sum1
sum1 = sum1 + num
return sum1
return inner
inner = outer()
print(inner(10))
print(inner(15))
print(inner(20))
2. Define a decorator : Print the time taken for the function to run
You get a time before execution
Execute function
Get a time after executing the function
To find the time difference
time modular
def outer(func):
def inner(*args):
data1 = time.time()
func(*args)
data2 = time.time()
print(data2 - data1)
return inner
@outer
def print_info(arg1):
print(arg1)
print_info([i for i in range(1000)])
Define a class :
requirement : Contains an object property , And use _( Underline ) Named
Define a class method ( Decorator )
Define a static method ( Decorator )
Define delegate properties ( Three class decorators ): You can visit _ The value of the named attribute
You can modify _ The value of the named attribute
You can delete _ Named properties
perform :
Instance an object
Calling class methods ( Class and object )
Call static methods ( Class and object )
object . Delegate properties
object . Delegate properties = value
del object . Delegate properties
class Person:
def __init__(self, name, age, sex):
self._name = name
self.age = age
self.sex = sex
@staticmethod
def run():
print(' Assad is ')
@classmethod
def lack(cls):
print(cls)
def get_item(self):
return self._name
def set_item(self, value):
self._name = value
def del_item(self):
del self._name
name = property(get_item, set_item, del_item)
per = Person(" Wang XX ", 60, ' male ')
per.run()
Person.run()
per.lack()
Person.lack()
print(per.name)
per.name = 123
print(per.name)
del per.name
print(per.name)
边栏推荐
- 搭建一个网站都需要那些东西
- Application case of CS brand SD NAND in air quality inspection industry
- 小迪网络安全笔记 信息收集-CDN绕过技术(7)
- 论文阅读:Deep Residual Learning in Spiking Neural Networks
- 传奇游戏架设教程
- 论文阅读:Deep Residual Shrinkage Networksfor Fault Diagnosis
- Commande awk du troisième épéiste - - interception
- edit关闭保存时自动生成配置文件
- notepad++下划线以及大小写字母置换
- Xiaodi network security - note encryption coding algorithm (6)
猜你喜欢
[automated testing] - robotframework practice (III) writing test cases
How to determine the electronic gear ratio of servo motor?
Wireshark packet capture: message information
Text three swordsman's awk command -- interception
[jmeter] TCP Sampler
中国知网论文free下载的方法
Servlet 笔记
Gnome 安装扩展插件(40.1版本,opensuse tumbleweed)。
Xiaodi network security - Notes (5)
我的世界1.12.2 神奇宝贝(精灵宝可梦) 开服教程
随机推荐
Good partner of single chip microcomputer - CS Genesis SD NAND flash
Slag learning path (1) output the calendar page of a certain month of a certain year
Wireshark packet capturing: error analysis
数据保护/磁盘列阵RAID保护 IP段103.103.188.xxx
Xiaodi network security - Notes (3)
Freebsd 12 安装RPM包
103.53.124.X IP段BGP线路和普通的专线有什么区别
5G时代服务器在这里面起着什么作用?
[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
类与super、继承
postgresql 数据库如何设置主键自增长
Configure raspberry pie 3b+ build a personal website
Arm server building my world (MC) version 1.18.2 private server tutorial
Xiaodi network security - Notes (5)
How to set primary key self growth in PostgreSQL database
Commande awk du troisième épéiste - - interception
[automated testing] - robotframework practice (I) building environment
论文阅读:Deep Residual Learning in Spiking Neural Networks
About file upload and download
Manual string comparison (pointer question)