python魔法方法
1.__call__
实现__call__后,该类的对象可以被调用
举例如:
class test_call_:
def __init__(self, n):
self.n = n
def __call__(self, *args, **kwargs):
print("print",self.n)
self.n +=1
test_call_obj = test_call_(1)
test_call_obj()
test_call_obj()
test_call_obj是类的对象,test_call_实现了__call__后,可以被该类的对象可以被调用,调用后会打印self.n的值
2.__repr__和__str__
repr和str的功能类似,像java的tostring,在print的时候被调用
官网链接:https://docs.python.org/3/reference/datamodel.html?highlight=__str__#object.__repr__
官网上对于__str__的解释,会在format()和print()时被调用,返回值必须是string类型

对于__repr__的定义

注意点:
1.repr在官网上推荐使用为返回值是一个调试类的信息
2.如果类没有实现__str__时 __repr__也会被调用
3.当类的对象被封装到容器中时,会优先调用__repr__,而不是__str__,这个在python官网中有人已经提出来了
https://www.python.org/dev/peps/pep-3140/
针对第3点举例如下
class test_str:
def __repr__(self):
return "repr"
def __str__(self):
return "str" if __name__=="__main__":
test_strObj = test_str()
print([test_strObj])
打印结果为:[repr]
而实际上按照python的做法应该优先调用__str__
print调用__str__和__repr__方法实例
实例1
class test_str:
def __repr__(self):
return "repr"
if __name__=="__main__":
test_strObj = test_str()
print(test_strObj)
结果是打印repr
实例2
class test_str:
# def __repr__(self):
# return "repr"
def __str__(self):
return "str"
if __name__=="__main__":
test_strObj = test_str()
print(test_strObj)
结果是打印str
class test_str:
def __repr__(self):
return "repr"
def __str__(self):
return "str" if __name__=="__main__":
test_strObj = test_str()
print(test_strObj)
结果仍然是打印str,说明,如果存在__str__时会优先调用__str__方法
4.eval
该方法不是魔法方法
eval(expression, globals=None, locals=None)
函数入参为string的表达式,eval会去执行这个字符串并得到返回值
subString = "w>2 and h>1"
r = eval(subString.replace("w","").replace("h",""))
print(r)
打印结果是True,表达式为"3>2 and 4>1",python执行后返回是True
print(eval('1+2'))
打印结果是3
python魔法方法的更多相关文章
- Python魔法方法总结及注意事项
1.何为魔法方法: Python中,一定要区分开函数和方法的含义: 1.函数:类外部定义的,跟类没有直接关系的:形式: def func(*argv): 2.方法:class内部定义的函数(对象的方法 ...
- python魔法方法:__getattr__,__setattr__,__getattribute__
python魔法方法:__getattr__,__setattr__,__getattribute__ 难得有时间看看书....静下心来好好的看了看Python..其实他真的没有自己最开始想的那么简单 ...
- python魔法方法大全
1.python魔法方法详解: python魔法方法是可以修改重载的,如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会在特殊的情况下被 Python 所调用,你可以定义自己想要的行为,而 ...
- python 魔法方法补充(__setattr__,__getattr__,__getattribute__)
python 魔法方法补充 1 getattribute (print(ob.name) -- obj.func())当访问对象的属性或者是方法的时候触发 class F(object): def _ ...
- 1. Python 魔法方法
Python 魔法方法 基础: 如果你想... 所以,你写... Python调用... 初始化一个实例 x = MyClass() x.__init__() 作为一个字符串的"官方&quo ...
- with上下文管理 python魔法方法
with语法在Python里很常见, 主要的利好是使用代码更简洁. 常见的使用场景有: 1. 资源对象的获取与释放. 使用with可以简化try...finally ... 2. 在不修改函数代码的前 ...
- python 魔法方法诠释
什么是Python魔法方法 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是两个下划线包围来命名的(比如 ini ...
- python 魔法方法
I am not a creator, I just a porter. Note: Everything is object in python. 对于Python来说一切都是对象,也就是函数的参数 ...
- python 魔法方法之:__getitem__ __setitem__ __delitem__
h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...
- python 魔法方法(学习过程的笔记)
有小伙伴会问,什么是python的魔法方法,python的魔法方法有什么用呢, 它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是两 ...
随机推荐
- 编码原则 之 Separation of Concerns
相关链接: Separation of Concerns 原文 The Art of Separation of Concerns Introduction In software engineeri ...
- 【转载】Xpath定位方法深入探讨及元素定位失败常见情况
一.Xpath定位方法深入探讨 (1)常用的Xpath定位方法及其特点 使用绝对路径定位元素. 例如: driver.findElement(By.xpath("/html/body/div ...
- 【转】 pthread设置线程的调度策略和优先级
转自:https://www.cnblogs.com/tianzeng/p/9192706.html 线程的调度有三种策略:SCHED_OTHER.SCHED_RR和SCHED_FIFO.Policy ...
- linux软件管理之概述
软件包管理 ====================================================================================安装/查询/卸载 一 ...
- 『Python CoolBook』数据结构和算法_多变量赋值&“*”的两种用法
多变量赋值 a = [1,2,(3,4)] b,c,d = a print(b,c,d) b,c,(d,e) = a print(b,c,d,e) 1 2 (3, 4) 1 2 3 4 a = &qu ...
- Python学习——多线程,异步IO,生成器,协程
Python的语法是简洁的,也是难理解的. 比如yield关键字: def fun(): for i in range(5): print('test') x = yield i print('goo ...
- linux 使用split分割大文件
1.分割 -- split命令 可以指定按行数分割和按字节大小分割两种模式. (1) 按行数分割 $ split -l 300 large_file.txt new_file_prefix 加上-d, ...
- Win10系列:C#应用控件进阶1
线形 线形没有内部空间,若要呈现一条直线,需要用Line对象的Stroke和StrokeThickness 属性分别为其轮廓的颜色及轮廓的粗细赋值,若不设置这两个属性,线形将不会呈现.绘制一条线形图形 ...
- jsp九大内置对象 ,三大指令,四大作用域,七大动作
九大内置对象: application:应用程序对象 对整个web工程都有效 request:对当前请求的封装 pageConfig:只对当前页面有效,里面封装了基本request和session的对 ...
- Permission denied (publickey). fatal: Could not read from remote repository.
博主在github上下载tiny face的的源代码的时候,遇到git clone命令为:git clone --recursive git@github.com:peiyunh/tiny.git 而 ...