• 技术文章 >Python技术 >Python基础教程

    Python魔术方法的三个特点

    小妮浅浅小妮浅浅2021-05-26 09:41:48原创1749

    1、特点

    (1)定义在class中

    (2)不需要直接调用

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

    2、实例

    class Person(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电脑。

    专题推荐:python魔术方法
    品易云
    上一篇:Python类成员的访问限制 下一篇:Python中装饰属性的方法

    相关文章推荐

    • Python描述器中__getattribute__调用• Python函数调用跟踪装饰器• Python双向队列是什么• Python如何标识线程?• Python实例属性的优先级分析• Python类成员的访问限制

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网