无善无恶是心之体,有善有恶是意之动,知善知恶是良知,为善去恶是格物
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()