当前位置: 首页 > 图灵资讯 > 行业资讯> Python魔术方法的三个特点

Python魔术方法的三个特点

发布时间:2024-08-21 22:24:49

1、特点

(1)在class中定义

(2)不需要直接调用

(3)Python的某些函数或操作符会调用相应的特殊方法

2、实例

classPerson(object):
def__init__(self,name,gender):
self.name=name
self.gender=gender

def__str__(self):
return'person:%s,%s'%(self.name,self.gender)

p=Person('zhangsan','male')

print(p)
#输出person:zhangsan,male

以上是Python魔法方法的三个特点,希望对大家有所帮助。更多Python学习推荐:python教学

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12