心即理、知行合一、致良知 with python

无善无恶是心之体,有善有恶是意之动,知善知恶是良知,为善去恶是格物

python 复制代码
class Goodness:
    def __init__(self, knowledge):
        self.knowledge = knowledge

    def reflect(self):
        print("Reflecting on the goodness of knowledge...")
        if self.knowledge:
            print("The knowledge is good.")
        else:
            print("The knowledge is lacking.")

class Nature:
    def __init__(self, object_name):
        self.object_name = object_name

    def observe(self):
        print(f"Observing the nature of {self.object_name}...")
        # Here you can add more complex observations or behaviors based on the nature of the object


def main():
    # Instantiate a Goodness object representing the goodness of knowledge
    goodness_of_knowledge = Goodness(knowledge=True)
    # Reflect on the goodness of knowledge
    goodness_of_knowledge.reflect()

    # Instantiate a Nature object representing the nature of a specific object
    nature_of_object = Nature(object_name="Python code")
    # Observe the nature of the object
    nature_of_object.observe()

if __name__ == "__main__":
    main()

加上装饰器

python 复制代码
def emphasize(func):
    def wrapper(*args, **kwargs):
        print("-" * 30)
        func(*args, **kwargs)
        print("-" * 30)
    return wrapper

class Goodness:
    def __init__(self, knowledge):
        self.knowledge = knowledge

    @emphasize
    def reflect(self):
        print("Reflecting on the goodness of knowledge...")
        if self.knowledge:
            print("The knowledge is good.")
        else:
            print("The knowledge is lacking.")

class Nature:
    def __init__(self, object_name):
        self.object_name = object_name

    @emphasize
    def observe(self):
        print(f"Observing the nature of {self.object_name}...")
        # Here you can add more complex observations or behaviors based on the nature of the object


def main():
    # Instantiate a Goodness object representing the goodness of knowledge
    goodness_of_knowledge = Goodness(knowledge=True)
    # Reflect on the goodness of knowledge
    goodness_of_knowledge.reflect()

    # Instantiate a Nature object representing the nature of a specific object
    nature_of_object = Nature(object_name="Python code")
    # Observe the nature of the object
    nature_of_object.observe()

if __name__ == "__main__":
    main()
相关推荐
我是华为OD~HR~栗栗呀几秒前
华为OD-23届考研-Java面经
java·c++·后端·python·华为od·华为·面试
yan8626592461 分钟前
于 C++ 的虚函数多态 和 模板方法模式 的结合
java·开发语言·算法
Small___ming16 分钟前
【Python基础】Python路径操作全解析:os.path、glob与pathlib从入门到精通
开发语言·python
这里有鱼汤21 分钟前
3步用Python识别MACD背驰,避免80%追涨杀跌陷阱,建议收藏
后端·python
程序员爱钓鱼42 分钟前
Python编程实战 · 基础入门篇 | Python能做什么
后端·python·github
_poplar_1 小时前
15 【C++11 新特性】统一的列表初始化和变量类型推导
开发语言·数据结构·c++·git·算法
lly2024061 小时前
Ruby Socket 编程
开发语言
i学长的猫1 小时前
Ruby小白学习路线
开发语言·学习·ruby
生信小窝1 小时前
068B-基于R语言平台Biomod2集成模型的物种分布模型构建和数据可视化教程【2027】
开发语言·信息可视化·r语言
一车小面包1 小时前
Transformers中从 logits 本质到问答系统中的字符定位机制
pytorch·python·深度学习