Python implements a solution to dynamically add class attributes or member functions. python Functions

Source: Internet
Author: User

Python implements a solution to dynamically add class attributes or member functions. python Functions

Sometimes we need to add attributes or methods to the class dynamically. For example, we can use this method when doing plug-ins. Use a configuration file to specify the modules to be loaded. You can add the required modules as needed.

This article briefly describes how to dynamically add class attributes or member functions in Python. The specific methods are as follows:

First, we can refer to the ulipad implementation: mixin.

This is relatively simple. It just declares a class. When the class is initialized, it reads the configuration file and loads the functions under the modules in a specific directory according to the configuration list. The functions have the same name as the modules, dynamically load this function as a member function of the class.

The Code is as follows:

class WinBAS(Bas):  def __init__(self):    self.__baslist = {}    self.__Init_Modules()    pass  def __Init_Modules(self):    import modplugs    for m in modplugs.__moduleset__:      mh = __import__('modules.' + m)# + '.' + m)      ma = getattr(mh, m)# + '.' + m)      ma = getattr(ma, m)      setattr(self.__class__, m, ma)

Modplugs. py is the module configuration file as follows:

__moduleset__ = ['BAS_GetUserList',]

Create an empty _ init _ under the modules directory __. py file to change the Directory into a package, and create a real BAS_GetUserList under the modules directory. The BAS_GetUserList function in the BAS_GetUserList file is as follows:

def BAS_GetUserList(self, strs):  return [0, strs]

In this way, the WinBAS class can dynamically Add the BAS_GetUserList function.


How to Implement Dynamic functions in python classes?

Here is an example of dynamically adding class functions.
Declare a class. When the class is initialized, read the configuration file and load the function under the module under the specified directory according to the configuration list. The function and the module have the same name, dynamically load this function as a member function of the class.
The Code is as follows:
Class WinBAS (Bas ):
Def _ init _ (self ):
Self. _ baslist = {}
Self. _ Init_Modules ()
Pass
Def _ Init_Modules (self ):
Import modplugs
For m in modplugs. _ moduleset __:
Mh = _ import _ ('Les Les. '+ m) # +'. '+ m)
Ma = getattr (mh, m) # + '.' + m)
Ma = getattr (ma, m)
Setattr (self. _ class __, m, ma)
Modplugs. py is the module configuration file as follows:
_ Moduleset _ = [
'Bas _ getuserlist ',
]
Create an empty _ init _ under the modules directory __. py file to change the Directory into a package, and create a real BAS_GetUserList under the modules directory. The BAS_GetUserList function in the BAS_GetUserList file is as follows:
Def BAS_GetUserList (self, strs ):
Return [0, strs]

In this way, the WinBAS class can dynamically Add the BAS_GetUserList function.

How does python define member functions outside the class?

The attribute values of a class can be set outside the class. You can also define out-of-class function. Yes. For example

Class:
Pass

Def hello (self, name ):
Print "Hello % s" % name

A. hello = hello
A (). hello ("somebody ")

This code is tested.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.