Reflection usage instance in python and python usage instance

Source: Internet
Author: User

Reflection usage instance in python and python usage instance

This example describes the reflection usage in python. Share it with you for your reference. The details are as follows:

import sys, types,newdef _get_mod(modulePath):  try:    aMod = sys.modules[modulePath]    if not isinstance(aMod, types.ModuleType):      raise KeyError  except KeyError:    # The last [''] is very important!    aMod = __import__(modulePath, globals(), locals(), [''])    sys.modules[modulePath] = aMod  return aModdef _get_func(fullFuncName):  """Retrieve a function object from a full dotted-package name."""  # Parse out the path, module, and function  lastDot = fullFuncName.rfind(u".")  funcName = fullFuncName[lastDot + 1:]  modPath = fullFuncName[:lastDot]  aMod = _get_mod(modPath)  aFunc = getattr(aMod, funcName)  # Assert that the function is a *callable* attribute.  assert callable(aFunc), u"%s is not callable." % fullFuncName  # Return a reference to the function itself,  # not the results of the function.  return aFuncdef _get_Class(fullClassName, parentClass=None):  """Load a module and retrieve a class (NOT an instance).  If the parentClass is supplied, className must be of parentClass  or a subclass of parentClass (or None is returned).  """  aClass = _get_func(fullClassName)  # Assert that the class is a subclass of parentClass.  if parentClass is not None:    if not issubclass(aClass, parentClass):      raise TypeError(u"%s is not a subclass of %s" %              (fullClassName, parentClass))  # Return a reference to the class itself, not an instantiated object.  return aClassdef applyFuc(obj,strFunc,arrArgs):  objFunc = getattr(obj, strFunc)  return apply(objFunc,arrArgs)def getObject(fullClassName):  clazz = _get_Class(fullClassName)  return clazz()if __name__=='__main__':  aa=getObject("inetservices.services.company.Company")    bb=applyFuc(aa, "select", ['select * from ngsys2',None])  print bb

I hope this article will help you with Python programming.

Related Article

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.