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中,对象 ...
随机推荐
- MySQL中IN()按照指定列指定规则排序
现在我有这么一个需求,我需要通过IN(id1,id2,......)查询id字段,并且id字段按照IN()中的顺序排序 例如:IN(5,1,2,4) ===> 查询出来的结果也应该为 5,1,2 ...
- postman——预处理和断言
一.预处理 Pre-request Scrip 1.Pre-request Script是集合中请求发送之前需要执行的代码片段 2.请求参数中包含一个随机数或者请求header中包括一个时间戳,或者你 ...
- Reactor 简介
官方的介绍如下: Reactor is a fully non-blocking reactive programming foundation for the JVM, with efficient ...
- 浅谈6种流行的API架构风格
前言 API在现代软件开发中扮演着重要的角色,它们是不同应用程序之间的桥梁.编写业务API是日常开发工作中最常见的一部分,选择合适的API框架对项目的成功起到了至关重要的作用.本篇文章将浅谈一下当前6 ...
- 加快脑动脉瘤检测,AI来了
摘要:华为云EI创新孵化Lab联合华中科技大学电信学院.华中科技大学同济医学院附属协和医院放射科在放射学领域的国际顶级期刊Radiology(<放射学>)上共同发表了最新研究成果. 日前, ...
- 海量监控数据处理如何做,看华为云SRE案例分享
摘要:openGemini的设计和优化都是根据时序数据特点而来,在面对海量运维监控数据处理需求时,openGemini显然更加有针对性. IT运维诞生于最早的信息化时代.在信息化时代,企业的信息化系统 ...
- 经典永流传,华为云媒体 AI 让老电影焕发新生
摘要:近日,在华为云TechWave全球技术峰会(人工智能&数据)上,马栏山视频文创产业园首席专家周苏岳受邀发表演讲<经典永流传,媒体 AI 让老电影焕发新生>,分享与华为云原生媒 ...
- vue2升级vue3:单文件组件概述 及 defineExpos/expose
像我这种react门徒被迫迁移到vue的,用管了TSX,地vue 单文件组件也不太感冒,但是vue3 单文件组件,造了蛮多api ,还不得去了解下 https://v3.cn.vuejs.org/ap ...
- html5鼠标拖动排序及resize实现方案分析及实践
对列表进行拖动排序,尺寸改变.之前一般会使用jQuery-UI.其通过mousedown.mousemove.mouseup这三个事件来实现页面元素被鼠标拖拽的效果.vue-drag-resize v ...
- 抖音APP如何实现用户生命周期提升
> 更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 近日,在火山引擎数智平台在北京举办的"超话数据:企业产品优化分享"的活动上,抖音策略 ...