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中,对象 ...
随机推荐
- 【scikit-learn基础】--『预处理』之 数据缩放
数据的预处理是数据分析,或者机器学习训练前的重要步骤.通过数据预处理,可以 提高数据质量,处理数据的缺失值.异常值和重复值等问题,增加数据的准确性和可靠性 整合不同数据,数据的来源和结构可能多种多样, ...
- HDU-2159 二维背包
最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级.现在的问题是,xhd升掉最后一级还需n的经 ...
- 【笔记整理】请求携带cookie的两种方法
""" 方法一:在请求头header中携带Cookie请求头信息(因为本身Cookie就是用请求头传递的....) 方法二:使用cookie参数传递cookie字典 &q ...
- SpringBoot 这么实现动态数据源切换,就很丝滑!
大家好,我是小富- 简介 项目开发中经常会遇到多数据源同时使用的场景,比如冷热数据的查询等情况,我们可以使用类似现成的工具包来解决问题,但在多数据源的使用中通常伴随着定制化的业务,所以一般的公司还是会 ...
- lottie 动画在 vue 中的使用
前言 最近我所负责的项目中,我采用了动画效果,并开始使用 gif 来实现.然而,在实践过程中,我发现 gif 格式的动画在 git 中出现了明显的锯齿感,这让我非常困扰.为了追求更完美的表现效果,我最 ...
- 虚拟化M搭建及基本操作
虚拟化MH搭建 虚拟化概念: 虚拟机安装分为2块:RHEVM .RHEVH RHEVM:负责管理角色 RHEVH:负责运算角色 2016-09-23_0-52-54.png hypervisor: 提 ...
- Flink实时处理入门
Flink实时处理入门 1.Flink框架介绍 Flink 诞生于欧洲的一个大数据研究项目 StratoSphere.它是由 3 所地处柏林的大学和欧洲其他一 些大学在 2010~2014 年共同进行 ...
- 花了1块钱体验一把最近很火的ChatGPT
前言 最近 OpenAI 发布了 ChatGPT,一经发布就在科技圈火得不行. ChatGPT是什么呢? 简单得说,ChatGPT,是一种基于对话的 AI 聊天工具.我们来看看ChatGPT自己得回答 ...
- 【DevCloud·敏捷智库】如何利用故事点做估算
背景 在某开发团队辅导的第二天,一个团队负责人咨询道:"领导经常管我要开发计划,我如何能快速的评估出预计开发完成时间呢,我们目前用工时估算,我听说过故事点估算,不知道适合吗?" 问 ...
- 小熊派开发实践丨漫谈LiteOS之传感器移植
摘要:本文基于小熊派开发板简单介绍了如何在LiteOS中移植传感器,从而实现对于传感器的相关控制. 1 hello world 相信大家无论在学习编程语言开始的第一个函数应该是HelloWorld,本 ...