擅长:python、mysql、java
<p>简而言之,您想向DBClient对象添加方法吗?为什么不动态添加呢?在</p>
<pre><code># defining the method to add
def query(self, command):
return self.select(command)
# Actually adding it to the DBClient class
DBClient.query = query
# Instances now come with the newly added method
db_client = DBClient()
# Using the method
return_command_1 = db_client.query("my_command_1")
return_command_2 = db_client.query("my_command_2")
</code></pre>
<p>归于<a href="http://igorsobreira.com/2011/02/06/adding-methods-dynamically-in-python.html" rel="nofollow">Igor Sobreira</a>。在</p>