Python学习第二十课——自定property and classmethod
自定制property
class Lazyproperty:
def __init__(self,func):
# print('==========>',func)
self.func=func
def __get__(self, instance, owner):
print('get')
# print(instance)
# print(owner)
if instance is None:
return self
res=self.func(instance)
setattr(instance,self.func.__name__,res)
return res
# def __set__(self, instance, value):
# pass class Room:
def __init__(self,name,width,length):
self.name=name
self.width=width
self.length=length
# @property #area=property(area)
@Lazyproperty #area=Lazypropery(area)
def area(self):
return self.width * self.length
@property #test=property(test)
def area1(self):
return self.width * self.length
# print(Room.__dict__)
r1=Room('厕所',1,1)
# print(r1.__dict__) #实例调用
# print(r1.area)
# print(Room.__dict__) #类调用
# print(Room.area) # print(r1.test)
# print(Room.test)
# print(r1.area1)
# print(r1.area1)
# print(r1.area1)
# print(r1.area1) print(r1.area)
print(r1.__dict__) print(r1.area)
print(r1.area)
print(r1.area)
print(r1.area)
print(r1.area)
print(r1.area)
print(r1.area)
print(r1.area)
自定制classmethod
class ClassMethod:
def __init__(self,func):
self.func=func
def __get__(self, instance, owner): #类来调用,instance为None,owner为类本身,实例来调用,instance为实例,owner为类本身,
def feedback(*args,**kwargs):
print('在这里可以加功能啊...')
return self.func(owner,*args,**kwargs)
return feedback class People:
name='linhaifeng'
@ClassMethod # say_hi=ClassMethod(say_hi)
def say_hi(cls,msg,x):
print('你好啊,帅哥 %s %s %s' %(cls.name,msg,x)) People.say_hi('你是那偷心的贼',10) p1=People()
p1.say_hi('你是那偷心的贼',10)
自定制元类
class MyType(type):
def __init__(self,a,b,c):
print('元类的构造函数执行')
# print(a)
# print(b)
# print(c)
def __call__(self, *args, **kwargs):
# print('=-======>')
# print(self)
# print(args,kwargs)
obj=object.__new__(self) #object.__new__(Foo)-->f1
self.__init__(obj,*args,**kwargs) #Foo.__init__(f1,*arg,**kwargs)
return obj
class Foo(metaclass=MyType): #Foo=MyType(Foo,'Foo',(),{})---》__init__
def __init__(self,name):
self.name=name #f1.name=name
# print(Foo)
# f1=Foo('alex')
# print(f1) f1=Foo('alex')
print(f1)
print(f1.__dict__)
Python学习第二十课——自定property and classmethod的更多相关文章
- Python学习第二十八课——Django(templates)
templates 讲后台得到的数据渲染到页面上:话不多说,先看具体代码. urls: from django.conf.urls import url from django.contrib imp ...
- Python学习第二十八课——Django(urls)
Django框架中的urls配置: 首先通过pycharm创建一个Django项目: 例如要写blog的功能:则在digango_lesson中的urls代码如下: """ ...
- Python学习第二十六课——PyMySql(python 链接数据库)
Python 链接数据库: 需要先安装pymysql 包 可以设置中安装,也可以pip install pymysql 安装 加载驱动: import pymysql # 需要先安装pymysql 包 ...
- Python学习第二十五课——Mysql (多表查询)
多表查询: 内连接查询: 首先:创建两个表一个为tableA,一个为tableB,并且插入数据(代码省略) 同时查询两个表的记录: select * from tableA,tableB; 根据tab ...
- Python学习第二十四课——Mysql 外键约束
外键:主要是关联两个表的 举个栗子:在建表中创建外键 -- 添加外键例子 CREATE TABLE teacher( id TINYINT PRIMARY KEY auto_increment, na ...
- Python学习第二十二课——Mysql 表记录的一些基本操作 (增删改)
记录基本操作: 增:(insert into) 基本语法: insert into 表名(字段) values(对应字段的值): 例子1: insert into employee(id,name,a ...
- python学习第二十九天函数局部变量如何改变外部变量
python函数局部变量如何改变外部变量,之前我说过,局部变量是没办法改变外部变量的,除非局部变量找不到,去外部找,输出变量,使用关键词global 使变量改变外部变量. 1,使用关键词global ...
- python学习第二十六天非固定参数几种情况
python函数参数传递,位置参数,默认参数,关键词参数,最后介绍一个非固定参数,就可以向函数传递一个列表,元组,字典,具体看看用法 1,有一个* 号的参数情况 def goos_stu(id,*us ...
- Python学习第二十七课——写一个和Django框架的自己的框架
MyWeb框架: from wsgiref.simple_server import make_server def application(environ, start_response): pri ...
随机推荐
- PAT-链表-A1032 Sharing
题意:给出两条链表的首地址以及若干个节点的的地址.数据.下一个节点的地址,求两条链表的首个共用节点的地址.如果两条链表没有共用节点,则输出-1. 思路:使用静态链表,首先遍历一遍第一个链表并进行标记. ...
- Java - 集合 - List
1.List实现类:ArrayList.LinkedList.Vector ArrayList使用: void test() { //声明 List<String> testlist = ...
- opencv:轮廓逼近与拟合
轮廓逼近,本质上是减少编码点 拟合圆,生成最相似的圆或椭圆 #include <opencv2/opencv.hpp> #include <iostream> using na ...
- bugku 好多压缩包
https://www.cnblogs.com/WangAoBo/p/6951160.html
- 题解【POJ1651】Multiplication Puzzle
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- Docker - Docker Engine 结构结构概述
概述 Docker Engine 结构的简单描述 ref docker 实战 第一本 docker 书 1. docker 版本 1. 版本 Docker Engine - Community 概述 ...
- 在 input 上添加图标字体时无法添加的问题
效果:一个搜索框.如图: 实施过程:一开始,将搜索框分为2部分,用2个 input ,一个 search ,一个 button ,然后给 type="button" 的input ...
- C++ explicit的作用
explicit作用: 在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: * e ...
- 在IDEA离线安装lombok插件
1.打开,找到自己IDEA版本,idea http://plugins.jetbrains.com/plugin/6317-lombok/versions 2.下载,导入安装
- JAVA基础学习(7)之函数
7函数 7.1函数定义与调用 7.1.1函数定义 7.1.2函数调用 package com.study.main; public class ObjectStudy { public static ...