心即理、知行合一、致良知 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()
相关推荐
湫ccc1 小时前
《Python基础》之字符串格式化输出
开发语言·python
mqiqe2 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql
AttackingLin2 小时前
2024强网杯--babyheap house of apple2解法
linux·开发语言·python
哭泣的眼泪4082 小时前
解析粗糙度仪在工业制造及材料科学和建筑工程领域的重要性
python·算法·django·virtualenv·pygame
Ysjt | 深2 小时前
C++多线程编程入门教程(优质版)
java·开发语言·jvm·c++
ephemerals__2 小时前
【c++丨STL】list模拟实现(附源码)
开发语言·c++·list
码农飞飞2 小时前
深入理解Rust的模式匹配
开发语言·后端·rust·模式匹配·解构·结构体和枚举
一个小坑货2 小时前
Rust 的简介
开发语言·后端·rust