首页 > 编程语言 >python: Decorators

python: Decorators

时间:2023-06-11 15:56:42浏览次数:36  
标签:printhello python setup printpy inner func Decorators

 

 

#装饰器
def printpy(func):
    def inner_func():
        func()
        print("hello python! Geovin Du")
    return inner_func
# @装饰器
@printpy
def printhello():
    print("hello world!")


#调用
printhello()

  

'''
Decorators.py file  装饰器
editor: geovindu, Geovin Du
date: 2023-06-11
'''

import sys


# setup 1: printpy(printhello), 调用printpy,把printhello当成参数传递进去:func=printhello
# setup 2: 定义内部函数inner_func
# setup 3: 返回内部函数inner_func
# setup 4: printhello=inner_func
# setup 5: printhello(),实际上调用内部函数inner_func
#装饰器
def printpy(func):
    def inner_func():
        func()
        print("hello python! Geovin Du")
    return inner_func
# @装饰器 Decorators
@printpy
def printhello():
    print("hello world!")

  

调用:

import Decorators

#调用
Decorators.printhello()
# setup 1: printpy(printhello), 调用printpy,把printhello当成参数传递进去:func=printhello
# setup 2: 定义内部函数inner_func
# setup 3: 返回内部函数inner_func
# setup 4: printhello=inner_func
# setup 5: printhello(),实际上调用内部函数inner_func
printhello=Decorators.printpy(Decorators.printhello)
printhello()

  

标签:printhello,python,setup,printpy,inner,func,Decorators
From: https://www.cnblogs.com/geovindu/p/17473042.html

相关文章

  • Python modbus_tk 库源码分析
    modbus_tk源代码分析前言modbus_tcp协议是工业项目中常见的一种基于TCP/IP协议的设备数据交互协议。作为TCP/IP协议的上层协议,modbus_tcp协议涉及到两个概念:client和server。但更标准的叫法应该是master和slave。Slave:TCP/IP协议中的server方Master:TCP/IP协......
  • 实验六 turtle绘图与python库应用编程体验
    1fromturtleimport*234defmove(x,y):5penup()6goto(x,y)7pendown()8910defdraw(n,size=100):11foriinrange(n):12fd(size)13left(360/n)141516defmain():17pensize(2)18pen......
  • python: generator
    a=10defaddgoled():globalaa+=1dd=[{'name':'zhang','age':10},{'name':'tu','age':13}]d={'name':'zhang','age':10}defadddict():globald#......
  • 实验6 turtle绘图与python库应用编程体验
    task1_11fromturtleimport*234defmove(x,y):5penup()6goto(x,y)7pendown()8910defdraw(n,size=100):11foriinrange(n):12fd(size)13left(360/n)141516defmain():17pensize(2)18......
  • Linux下安装python3.7.9
    操作系统与原有python[pythondemo@localhost~]$cat/etc/centos-releaseCentOSLinuxrelease7.4.1708(Core)[pythondemo@localhost~]$pythonPython2.7.5(default,Aug42017,00:39:18)[GCC4.8.520150623(RedHat4.8.5-16)]onlinux2Type"help",......
  • python第一次cli程序的坑
    错误一:appacheerror记录到错误:AH01215:(13)Permissiondenied:execof'/var/www/cgi-bin/cli_test.py'failed1.对应程序加上执行权限2.selinux=disable3.指定安全上下文 chcon-R-thttpd_sys_content_t/var/www/cgi-bin这三个方法可以试一下错误二:malformedheade......
  • 【技术积累】Python中的Pandas库【三】
    什么是SeriesSeries是一种带有标签的一维数组,可以容纳各种类型的数据(例如整数,浮点数和字符串)。每个Series对象都有一个索引,它可以用来引用每个元素。Series对象的主要特征是可以进行矢量化操作(即一次对整个序列进行操作),因此非常适合处理数值数据。什么是DataFrame?DataFrame是一......
  • Python实现猜拳小游戏的多种方式
    简介猜拳小游戏是一个经典的小游戏项目,也是初学者学习编程的必要练手题目之一。在Python中,我们可以使用多种方式来实现一个简单的猜拳小游戏。本文将依次介绍六种Python实现猜拳小游戏的方法,包括:使用if-else条件语句、使用random模块、使用字典映射胜负关系、for循环、whi......
  • python数组避坑操作(比如删除数组中的所有0)
    一、演示坑tracks=[0,0,0,1,1,1]fortrackintracks:iftrack==0:tracks.remove(track)print(tracks)#[0,1,1,1]发现:有一个0没有被删去,why???二、这次遍历时,带上索引打印tracks=[0,0,0,1,1,1]forindex,trackinenumerate(tracks......
  • python变量前的星号
    变量前单星号表示将参数转化成元组变量前双星号表示将参数转化成字典函数传参顺序从左到右(一般):位置参数、默认参数、单星号参数、关键字传参、双星号参数传参解压功能单星号对list或元组进行解压,输入的参数不是一个list或元组,而是其中的元素。双星号对字典进......