Python 学习笔记: 从变量到装饰器
从变量开始
>>> a = b = 1
>>> print(id(a), id(b))
265086896 265086896
def hello():
print("Hello World\n") lianhuasheng = hello
print(id(lianhuasheng), id(hello)) >>> 21004616 21004616
由于 python 是动态解释型语言,在执行到函数定义处,即在内存中给 hello 函数开辟内存,所以这里引用 lianhuasheng 和 hello 都指向了 hello 函数的内存地址 21004616。
def inputName(name):
hostname = name + ".local"
def hello():
print(hostname)
return hello name = inputName("lianhuasheng")
print(id(name), name, name.__name__) >>> 3637576 <function inputName.<locals>.hello at 0x00378148> hello
从打印结果可以看到,引用 name “指向”的是函数名为 hello 的内存地址。
def inputName(name):
hostname = name + ".local"
def hello():
print(hostname)
return hello name = inputName("lianhuasheng")
print(id(name), name, name.__name__)
name() >>> 55148872 <function inputName.<locals>.hello at 0x03498148> hello
>>> lianhuasheng.local
def inputName(name):
hostname = name + ".local"
def hello():
hostname = hostname + ".fullname"
print(hostname)
return hello name = inputName("lianhuasheng")
print(id(name), name, name.__name__)
name() >>> UnboundLocalError: local variable 'hostname' referenced before assignment def inputName(name):
hostname = name + ".local"
def hello():
nonlocal hostname
hostname = hostname + ".fullname"
print(hostname)
return hello name = inputName("lianhuasheng")
print(id(name), name, name.__name__)
name() >>> 46760264 <function inputName.<locals>.hello at 0x02C98148> hello
lianhuasheng.local.fullname
从闭包到装饰器
def hello():
print("Hello World") def helloDecorator(func):
print("This is a demo of decorator")
def wrapper(*args, **kw):
return func(*args, **kw)
return wrapper lianhuasheng = helloDecorator(hello)
print(lianhuasheng.__name__) >>> This is a demo of decorator
>>> wrapper
def helloDecorator(func):
print("This is a demo of decorator")
def wrapper(*args, **kw):
return func(*args, **kw)
return wrapper @helloDecorator
def hello():
print("Hello World") print(hello.__name__) >>> This is a demo of decorator
>>> wrapper
装饰器在类里是什么样呢?
class Demo:
name = "None" def __init__(self):
self.name = Demo.name
print("A demo of staticmethod and classmethod") @staticmethod
def printName(name):
print("My name is {}".format(name)) @classmethod
def inputName(cls, name):
cls.name = name
Demo.printName(cls.name)
print(cls) student = Demo()
student.inputName("lianhuasheng")
print(student.name, Demo.name, student, Demo) student.name = "lianhua"
print(student.name, Demo.name, student, Demo) Demo.inputName("lianhuasheng")
print(student.name, Demo.name, student, Demo) student.printName("lianhuasheng")
Demo.printName("lianhuasheng") >>>
A demo of staticmethod and classmethod
My name is lianhuasheng
<class '__main__.Demo'>
None lianhuasheng <__main__.Demo object at 0x00AF4E80> <class '__main__.Demo'>
lianhua lianhuasheng <__main__.Demo object at 0x00AF4E80> <class '__main__.Demo'>
My name is lianhuasheng
<class '__main__.Demo'>
lianhua lianhuasheng <__main__.Demo object at 0x00AF4E80> <class '__main__.Demo'>
My name is lianhuasheng
My name is lianhuasheng
- 类中,在定义前分别加上 @staticmethod 和 @classmethod 表示静态方法和类成员方法。
- 不管是静态方法和类成员方法都能被类实例和类访问。
- 静态方法不能修改类变量和类实例变量,且它接受的参数非 self /非 cls。相当于是定义在类中的函数。
- 类成员方法可以修改类变量,但是不能访问类实例变量。它传入的 cls 参数实际上是类,在上例中是 <class '__main__.Demo'>。
- 修改类实例变量的值并不会改变类变量,同样的修改类变量也不会改变类实例变量的值。
Python 学习笔记: 从变量到装饰器的更多相关文章
- python学习笔记(5)--迭代器,生成器,装饰器,常用模块,序列化
生成器 在Python中,一边循环一边计算的机制,称为生成器:generator. 如: >>> g = (x * x for xin range(10)) >>> ...
- python学习笔记-(八)装饰器、生成器&迭代器
本节课程内容概览: 1.装饰器 2.列表生成式&迭代器&生成器 3.json&pickle数据序列化 1. 装饰器 1.1 定义: 本质上是个函数,功能是装饰其他函数—就是为其 ...
- Python学习笔记(yield与装饰器)
yeild:返回一个生成器对象: 装饰器:本身是一个函数,函数目的装饰其他函数(调用其他函数) 功能:增强被装饰函数的功能 装饰器一般接受一个函数对象作为参数,以便对其增强 @原函数名 来调用其他函 ...
- python 基础学习笔记(8)--装饰器
**装饰器** - [ ] 装饰器和闭包有很大的联系.有时你需要在不改变源代码的情况下修改已经存在的函数.装饰器的运用可以提高效率,减少重复的代码. - [ ] 装饰器的实质是一个函数.它把一个函数作 ...
- python学习总结---函数使用 and 装饰器
# 函数使用 ### 零碎知识 - 灵活的if-else ```python a = 3 if False else 5 print(a) ''' if False: a = 3 else: a = ...
- Python学习基础(三)——装饰器,列表生成器,斐波那契数列
装饰器——闭包 # 装饰器 闭包 ''' 如果一个内部函数对外部(非全局)的变量进行了引用,那么内部函数被认为是闭包 闭包 = 函数块 + 定义时的函数环境 ''' def f(): x = 100 ...
- python学习之路 六 :装饰器
本节重点: 掌握装饰器相关知识 python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函 ...
- Python高级笔记(十一)装饰器【面试】
1. 需求 开发封闭原则:虽然在这个原则是用的面向对象开发,但是也适用于函数式编程,简单来说,它规定已经实现的功能代码不允许被修改,但可以被拓展,即: 封闭:已实现的功能代码块 开发:对拓展开发 2. ...
- Python学习日记(七)——装饰器
1.必备知识 #### 一 #### def foo(): print 'foo' foo #表示是函数 foo() #表示执行foo函数 #### 二 #### def foo(): print ' ...
- JavaScript学习笔记(四十四) 装饰器
装饰器模式(Decorator) 在装饰器模式中,可以在运行时给一个对象动态的添加额外的功能.当和静态类打交道的时候(static classes),这可能是一个挑战.但在JavaScript中,对象 ...
随机推荐
- catcat-new【目录穿透+特殊文件】
catcat-new[目录穿透+特殊文件] 题目界面 点击任何一只猫猫,发现路径泄露: 解题步骤 测试目录遍历漏洞 路径: ?file=../../../../etc/passwd 成功读取到pass ...
- 【分享】推荐一个非常好用的redis远程连接工具
推荐一个非常好用的redis远程连接工具 蓝奏云地址 https://wwsi.lanzoum.com/ig7xZ0xspf0h 密码:4vnz 二维码:
- 在k8s中快速搭建基于Prometheus监控系统
公众号「架构成长指南」,专注于生产实践.云原生.分布式系统.大数据技术分享 前言 K8s本身不包含内置的监控工具,所以市场上有不少这样监控工具来填补这一空白,但是没有一个监控工具有prometheus ...
- Bazel 如何生成 clangd/clang-tidy 所需的 compile_commands.json
VSCode 中如何使用 clang-tidy 安装 clangd 插件 禁用 ms-cpp 插件(VSCode 会自动提示有冲突) 生成 clangd 所需的 compile_commands.js ...
- 宝塔面板如何用一IP不同端口创建不同的网站(“您添加的站点已存在”)
问题描述 玩宝塔面板的时候,一开始没有云服务器,需要在本地虚拟机里搭建各种网站,想在本地服务器下搭建多个站点,但是总会遇到"您添加的站点已存在"这个现象. 问题原因及解决办法 出现 ...
- 鱼和熊掌兼得:C++代码在编译时完成白盒测试
摘要:如果能够让代码在编译的时候,自动完成白盒测试,这不是天方夜谭. 白盒测试也叫开发者测试,是对特定代码函数或模块所进行的功能测试.当前主流的白盒测试方法是:先针对仿真或者生产环境编译出可执行文件, ...
- 想发自己的NFT,你要先搞清楚这6个问题
摘要:NFT是Web3世界中标记数据资产独特性的标识,是数据权益的载体. 本文分享自华为云社区<加密数字艺术NFT背后你关心的六个问题>,作者: 薛腾飞 . Connect Wallet ...
- 手绘流程图讲解spark是如何实现集群的高可用
摘要:本文讲述spark是怎么针对master.worker.executor的异常情况做处理的. 本文分享自华为云社区<图解spark是如何实现集群的高可用>,作者:breakDawn. ...
- 梦幻联动!金蝶&华为云面向大企业发布数据库联合解决方案
摘要:近日,金蝶软件(中国)有限公司(以下简称"金蝶")携手华为云共同发布了金蝶云·星瀚.金蝶云·苍穹和GaussDB(for openGauss)数据库联合解决方案. 本文分享自 ...
- 华山论“件”:Kafka、RabbitMQ、RocketMQ技能大比拼
摘要:主流的消息中间件包含Kafka.RabbitMQ和RocketMQ,本期云图说为您介绍它们之前的差异. 本文分享自华为云社区<第234期 华山论"件"-Kafka.Ra ...