====== Python中遍历类的成员变量 ====== 在Python中经常会遇到新的类,有时候需要遍历一下类的成员变量和函数,用dir只能看到成员的名字,而且也不能区分成员是变量还是函数。 ====== Python类遍历实现代码 ====== 下面的Python代码就实现了遍历类成员,并将成员变量打印出来。 types = [type(i) for i in [0, "0", [0], {0:1}, (0,)]] content = "" for k in dir(self.request): content += "%s: %s\n" % (k, type(eval("self.request.%s" % k))) if type(eval("self.request.%s" % k)) in types: content += "%s\n" % str(eval("self.request.%s" % k)) content += "\n" print content ===== Google App Engine中request类的遍历 ===== GET: POST: ResponseClass: __class__: __delattr__: __dict__: {'charset': 'utf-8', 'decode_param_names': True, '_environ': {'wsgi.version': (1, 0), 'webob._parsed_query_vars': (MultiDict([]), ''), 'SERVER_SOFTWARE': 'Development/1.0', 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'HTTP_KEEP_ALIVE': '115', 'SERVER_PROTOCOL': 'HTTP/1.0', 'QUERY_STRING': '', 'CONTENT_LENGTH': '', 'HTTP_ACCEPT_CHARSET': 'GB2312,utf-8;q=0.7,*;q=0.7', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0', 'HTTP_CONNECTION': 'keep-alive', 'SERVER_NAME': 'localhost', 'REMOTE_ADDR': '127.0.0.1', 'wsgi.url_scheme': 'http', 'SDK_VERSION': '1.4.2', 'PATH_TRANSLATED': 'C:\\Documents and Settings\\dongu\\My Documents\\projects\\website\\gae\\vanclquan\\index.py', 'SERVER_PORT': '8080', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CURRENT_VERSION_ID': '2.1', 'USER_ORGANIZATION': '', 'wsgi.input': , 'HTTP_HOST': 'localhost:8080', 'wsgi.multithread': False, 'APPLICATION_ID': 'vanclquan', 'HTTP_CACHE_CONTROL': 'max-age=0', 'USER_EMAIL': '', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'USER_ID': '', 'GATEWAY_INTERFACE': 'CGI/1.1', 'wsgi.run_once': True, 'wsgi.errors': ', mode 'w' at 0x00BAF0B0>, 'wsgi.multiprocess': False, 'HTTP_ACCEPT_LANGUAGE': 'zh-cn,zh;q=0.5', 'AUTH_DOMAIN': 'gmail.com', 'TZ': 'UTC', 'PATH_INFO': '/'}, 'unicode_errors': 'ignore'} __doc__: Abstraction for an HTTP request. Properties: uri: the complete URI requested by the user scheme: 'http' or 'https' host: the host, including the port path: the path up to the ';' or '?' in the URL parameters: the part of the URL between the ';' and the '?', if any query: the part of the URL after the '?' You can access parsed query and POST values with the get() method; do not parse the query string yourself. __getattr__: __getattribute__: __hash__: __init__: __module__: google.appengine.ext.webapp __new__: __reduce__: __reduce_ex__: __repr__: __setattr__: __str__: __weakref__: _body__del: _body__get: _body__set: _body_file__del: _body_file__get: _body_file__set: _cache_control__del: _cache_control__get: _cache_control__set: _environ: {'wsgi.version': (1, 0), 'webob._parsed_query_vars': (MultiDict([]), ''), 'SERVER_SOFTWARE': 'Development/1.0', 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'HTTP_KEEP_ALIVE': '115', 'SERVER_PROTOCOL': 'HTTP/1.0', 'QUERY_STRING': '', 'CONTENT_LENGTH': '', 'HTTP_ACCEPT_CHARSET': 'GB2312,utf-8;q=0.7,*;q=0.7', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0', 'HTTP_CONNECTION': 'keep-alive', 'SERVER_NAME': 'localhost', 'REMOTE_ADDR': '127.0.0.1', 'wsgi.url_scheme': 'http', 'SDK_VERSION': '1.4.2', 'PATH_TRANSLATED': 'C:\\Documents and Settings\\dongu\\My Documents\\projects\\website\\gae\\vanclquan\\index.py', 'SERVER_PORT': '8080', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CURRENT_VERSION_ID': '2.1', 'USER_ORGANIZATION': '', 'wsgi.input': , 'HTTP_HOST': 'localhost:8080', 'wsgi.multithread': False, 'APPLICATION_ID': 'vanclquan', 'HTTP_CACHE_CONTROL': 'max-age=0', 'USER_EMAIL': '', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'USER_ID': '', 'GATEWAY_INTERFACE': 'CGI/1.1', 'wsgi.run_once': True, 'wsgi.errors': ', mode 'w' at 0x00BAF0B0>, 'wsgi.multiprocess': False, 'HTTP_ACCEPT_LANGUAGE': 'zh-cn,zh;q=0.5', 'AUTH_DOMAIN': 'gmail.com', 'TZ': 'UTC', 'PATH_INFO': '/'} _environ_getter: _headers: _headers__get: _headers__set: _host__del: _host__get: _host__set: _urlargs__del: _urlargs__get: _urlargs__set: _urlvars__del: _urlvars__get: _urlvars__set: accept: accept_charset: accept_encoding: accept_language: application_url: http://localhost:8080 arguments: blank: body: body_file: cache_control: call_application: charset: utf-8 content_length: content_type: application/x-www-form-urlencoded cookies: copy: copy_get: date: decode_param_names: environ: {'wsgi.version': (1, 0), 'webob._parsed_query_vars': (MultiDict([]), ''), 'SERVER_SOFTWARE': 'Development/1.0', 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'HTTP_KEEP_ALIVE': '115', 'SERVER_PROTOCOL': 'HTTP/1.0', 'QUERY_STRING': '', 'CONTENT_LENGTH': '', 'HTTP_ACCEPT_CHARSET': 'GB2312,utf-8;q=0.7,*;q=0.7', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0', 'HTTP_CONNECTION': 'keep-alive', 'SERVER_NAME': 'localhost', 'REMOTE_ADDR': '127.0.0.1', 'wsgi.url_scheme': 'http', 'SDK_VERSION': '1.4.2', 'PATH_TRANSLATED': 'C:\\Documents and Settings\\dongu\\My Documents\\projects\\website\\gae\\vanclquan\\index.py', 'SERVER_PORT': '8080', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CURRENT_VERSION_ID': '2.1', 'USER_ORGANIZATION': '', 'wsgi.input': , 'HTTP_HOST': 'localhost:8080', 'wsgi.multithread': False, 'APPLICATION_ID': 'vanclquan', 'HTTP_CACHE_CONTROL': 'max-age=0', 'USER_EMAIL': '', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'USER_ID': '', 'GATEWAY_INTERFACE': 'CGI/1.1', 'wsgi.run_once': True, 'wsgi.errors': ', mode 'w' at 0x00BAF0B0>, 'wsgi.multiprocess': False, 'HTTP_ACCEPT_LANGUAGE': 'zh-cn,zh;q=0.5', 'AUTH_DOMAIN': 'gmail.com', 'webob._cache_control': ('max-age=0', ), 'TZ': 'UTC', 'webob._parsed_cookies': ({}, ''), 'PATH_INFO': '/'} get: get_all: get_range: get_response: headers: host: localhost:8080 host_url: http://localhost:8080 if_match: if_modified_since: if_none_match: if_range: if_unmodified_since: is_xhr: max_forwards: method: GET params: path: / path_info: / path_info_peek: path_info_pop: path_qs: / path_url: http://localhost:8080/ postvars: pragma: query: query_string: queryvars: range: referer: referrer: relative_url: remote_addr: 127.0.0.1 remote_user: remove_conditional_headers: request_body_tempfile_limit: 0 scheme: http script_name: server_name: localhost server_port: 8080 str_GET: str_POST: str_cookies: {} str_params: str_postvars: str_queryvars: unicode_errors: ignore uri: http://localhost:8080/ url: http://localhost:8080/ urlargs: () urlvars: {} user_agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0