Py学习  »  Python

python创建自定义魔术方法

Jasonca1 • 7 年前 • 2106 次点击  

在python中,如果我想创建一个对象,它在传递给特定函数时做了一些事情,我该怎么做呢? 例如:

import logging
class  TestObject:
    def __logging__(self):
        # It would check the calling function here and return something 
        # if it was a log function call from a logging class log function call
        return 'Some text'
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40535
文章 [ 2 ]  |  最新文章 7 年前
Fouad Grimate
Reply   •   1 楼
Fouad Grimate    7 年前

` 定义日志(CLS): 返回CLS。 登录中 ()

CLS类: DEF 登录中 (自我): return“调用了日志记录”

c = 打印(日志(c)) `

Patrick Haugh
Reply   •   2 楼
Patrick Haugh    7 年前
def logging(cls):
  return cls.__logging__()

class Cls:
  def __logging__(self):
    return "__logging__ called"