当前位置:网站首页>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)
边栏推荐
- Redraiment的遭遇
- 【无标题】
- 数据保护/磁盘列阵RAID保护 IP段103.103.188.xxx
- Xiaodi network security - note encryption coding algorithm (6)
- University
- 传奇怎么开服?开传奇私服需要准备什么呢?
- Performance test and price comparison of cloud servers of Alibaba cloud, Tencent cloud, Huawei cloud, ucloud and Tianyi cloud
- Minecraft Paper 1.18.1 版开服教程,我的世界开服教程,MCSManager9面板使用教程
- 类与对象
- The use and differences of dictionaries, tuples and lists,
猜你喜欢

opensuse 安装网易云音乐(tumbleweed)(leap)

Performance evaluation and comparison of Huawei cloud Kunpeng arm ECs and x86 ECS

传奇游戏架设教程

Servlet 笔记

Xiaodi network security - Notes (5)

论文阅读:Deep Residual Shrinkage Networksfor Fault Diagnosis

Minecraft Paper 1.18.1 版开服教程,我的世界开服教程,MCSManager9面板使用教程

UCloud(优刻得) 上海 ARM 云服务器评测

Évaluation des performances de la machine virtuelle Tianyi Cloud Hangzhou (VPS)

【自动化测试】——robotframework实战(二)新建测试用例
随机推荐
Comparison between CS brand SD NAND and SPI NAND
Minecraft bedrock BDS service tutorial
寄居蟹和海葵
Class is coming. Roll call is needed
pytorch张量
破译密码(综合)
银河麒麟服务器系统搭建本地和局域网yum源
Intranet penetration server building tutorial, NPs use tutorial
Application case of CS brand SD NAND in air quality inspection industry
[automated testing] - robotframework practice (III) writing test cases
How to determine the electronic gear ratio of servo motor?
天翼云 杭州 云主机(VPS) 性能评测
Minecraft Paper 1.18.1 版开服教程,我的世界开服教程,MCSManager9面板使用教程
Escape from the center of the lake (math problem)
Wireshark packet capturing: error analysis
Gnome 安装扩展插件(40.1版本,opensuse tumbleweed)。
小迪网络安全-笔记 加密编码算法(6)
The use and differences of dictionaries, tuples and lists,
明明爱喝水
IP103.53.125.xxx IP地址段 详解