#!/usr/bin/env python #-*- encoding:utf-8 -*- #------------------------ # Refrer : # http://www.cnblogs.com/pylemon/archive/2011/06/09/2076862.html #------------------------ # æè¿çDive into python第åç« èªç䏿å°getattr()彿°,ä½ä¸ºä¸ä¸ªå å»ºå½æ°å¹³æ¶èªå·±æ²¡æä¹ç¨è¿æä»¥ä¹ä¸å¤ªçè§£è¿ä¸ªå½æ°çä¸äºç¨æ³ # çäºä¸å½æ°æ¬èº«çdoc # getattr(object, name[, default]) -> value # Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. # When a default argument is given, it is returned when the attribute doesn't # exist; without it, an exception is raised in that case. # è§£éç徿½è±¡ åè¯æè¿ä¸ªå½æ°çä½ç¨ç¸å½äºæ¯ # object.name # è¯äºä¸ä¸getattr(object,name)ç¡®å®åobject.nameæ¯ä¸æ ·çåè½.åªä¸è¿è¿éå¯ä»¥ænameä½ä¸ºä¸ä¸ªåéå»å¤ç # 书ä¸çä¾åå¾å¥½ç说æäºè¿ä¸ªå½æ°çåç¨ # 使ç¨getattrå¯ä»¥è½»æ¾å®ç°å·¥å模å¼ã # ä¾ï¼ä¸ä¸ªæ¨¡åæ¯æhtmlãtextãxmlçæ ¼å¼çæå°ï¼æ ¹æ®ä¼ å ¥çformateåæ°çä¸åï¼è°ç¨ä¸åç彿°å®ç°å ç§æ ¼å¼çè¾åº # 1 # 2 # 3 # 4 # import statsout # def output(data, format="text"): # output_function = getattr(statsout, "output_%s" %format) # return output_function(data) # è¿ä¸ªä¾åä¸å¯ä»¥æ ¹æ®ä¼ å ¥output彿°çformatåæ°çä¸å å»è°ç¨statsout模åä¸åçæ¹æ³(ç¨æ ¼å¼åå符串å®ç°output_%s) # è¿åçæ¯è¿ä¸ªæ¹æ³ç对象 å°±å¯ä»¥ç´æ¥ä½¿ç¨äº å¦æè¦æ·»å æ°çæ ¼å¼ åªéè¦å¨æ¨¡åä¸åå ¥æ°çæ¹æ³å½æ° å¨è°ç¨output彿°æ¶ä½¿ç¨æ°çåæ°å°±å¯ä»¥ä½¿ç¨ä¸åçæ ¼å¼è¾åº # ç¡®å®å¾æ¹ä¾¿ # 为äºå 深对getattr彿°ççè§£ 转载ä¸ç¯è±æç说æ # Pythonâs getattr function is used to fetch an attribute from an object, using a string object instead of an identifier to identify the attribute. In other words, the following two statements are equivalent: # value = obj.attribute # value = getattr(obj, "attribute") # If the attribute exists, the corresponding value is returned. If the attribute does not exist, you get an AttributeError exception instead. # The getattr function can be used on any object that supports dotted notation (by implementing the __getattr__ method). This includes class objects, modules, and even function objects. # path = getattr(sys, "path") # doc = getattr(len, "__doc__") # The getattr function uses the same lookup rules as ordinary attribute access, and you can use it both with ordinary attributes and methods: # result = obj.method(args) # func = getattr(obj, "method") # result = func(args) # or, in one line: # result = getattr(obj, "method")(args) # Calling both getattr and the method on the same line can make it hard to handle exceptions properly. To avoid confusing AttributeError exceptions raised by getattr with similar exceptions raised inside the method, you can use the following pattern: # try: # func = getattr(obj, "method") # except AttributeError: # ... deal with missing method ... # else: # result = func(args) # The function takes an optional default value, which is used if the attribute doesnât exist. The following example only calls the method if it exists: # func = getattr(obj, "method", None) # if func: # func(args) # Hereâs a variation, which checks that the attribute is indeed a callable object before calling it. # func = getattr(obj, "method", None) # if callable(func): # func(args)