首页 > 编程语言 > 详细

python 的class和def 定义执行语句相关

时间:2021-04-06 12:54:01      阅读:16      评论:0      收藏:0      [点我收藏+]
  1. def 函数定义不会执行里面的语句
  2. class 类的定义会执行里面的语句
def test_func():
    print("test_func")          # 不会执行


class setter(object):

    def __init__(self, method):
        self.method = method
        print(‘setter: __init__:‘ ,method)


class Keyword(object):
    class_ = ‘test class‘       # 会执行
    print(class_)               # 会执行

    @setter                     # 会执行 (重点注意)
    def parent(self, parent):   # 不会执行
        print(‘test func‘)



‘‘‘ 以下不实例化也会打印
test classs
setter: __init__: <function Keyword.parent at 0x0000025FF306A678>
‘‘‘

python 的class和def 定义执行语句相关

原文:https://www.cnblogs.com/amize/p/14620680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!