在Python中经常会遇到新的类,有时候需要遍历一下类的成员变量和函数,用dir只能看到成员的名字,而且也不能区分成员是变量还是函数。
下面的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
GET: <type 'instance'> POST: <type 'instance'> ResponseClass: <type 'type'> __class__: <type 'type'> __delattr__: <type 'instancemethod'> __dict__: <type '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': <cStringIO.StringI object at 0x045C31E8>, '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': <open file '<stderr>', 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__: <type 'str'> 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__: <type 'instancemethod'> __getattribute__: <type 'method-wrapper'> __hash__: <type 'method-wrapper'> __init__: <type 'instancemethod'> __module__: <type 'str'> google.appengine.ext.webapp __new__: <type 'builtin_function_or_method'> __reduce__: <type 'builtin_function_or_method'> __reduce_ex__: <type 'builtin_function_or_method'> __repr__: <type 'instancemethod'> __setattr__: <type 'instancemethod'> __str__: <type 'instancemethod'> __weakref__: <type 'NoneType'> _body__del: <type 'instancemethod'> _body__get: <type 'instancemethod'> _body__set: <type 'instancemethod'> _body_file__del: <type 'instancemethod'> _body_file__get: <type 'instancemethod'> _body_file__set: <type 'instancemethod'> _cache_control__del: <type 'instancemethod'> _cache_control__get: <type 'instancemethod'> _cache_control__set: <type 'instancemethod'> _environ: <type 'dict'> {'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': <cStringIO.StringI object at 0x045C31E8>, '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': <open file '<stderr>', 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: <type 'instancemethod'> _headers: <type 'NoneType'> _headers__get: <type 'instancemethod'> _headers__set: <type 'instancemethod'> _host__del: <type 'instancemethod'> _host__get: <type 'instancemethod'> _host__set: <type 'instancemethod'> _urlargs__del: <type 'instancemethod'> _urlargs__get: <type 'instancemethod'> _urlargs__set: <type 'instancemethod'> _urlvars__del: <type 'instancemethod'> _urlvars__get: <type 'instancemethod'> _urlvars__set: <type 'instancemethod'> accept: <class 'webob.acceptparse.MIMEAccept'> accept_charset: <class 'webob.acceptparse.Accept'> accept_encoding: <class 'webob.acceptparse.NoAccept'> accept_language: <class 'webob.acceptparse.Accept'> application_url: <type 'str'> http://localhost:8080 arguments: <type 'instancemethod'> blank: <type 'instancemethod'> body: <type 'str'> body_file: <type 'cStringIO.StringI'> cache_control: <class 'webob.cachecontrol.CacheControl'> call_application: <type 'instancemethod'> charset: <type 'str'> utf-8 content_length: <type 'NoneType'> content_type: <type 'str'> application/x-www-form-urlencoded cookies: <type 'instance'> copy: <type 'instancemethod'> copy_get: <type 'instancemethod'> date: <type 'NoneType'> decode_param_names: <type 'bool'> environ: <type 'dict'> {'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': <cStringIO.StringI object at 0x045C31E8>, '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': <open file '<stderr>', 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', <CacheControl 'max-age=0'>), 'TZ': 'UTC', 'webob._parsed_cookies': ({}, ''), 'PATH_INFO': '/'} get: <type 'instancemethod'> get_all: <type 'instancemethod'> get_range: <type 'instancemethod'> get_response: <type 'instancemethod'> headers: <type 'instance'> host: <type 'str'> localhost:8080 host_url: <type 'str'> http://localhost:8080 if_match: <class 'webob.etag._AnyETag'> if_modified_since: <type 'NoneType'> if_none_match: <class 'webob.etag._NoETag'> if_range: <class 'webob.etag._NoIfRange'> if_unmodified_since: <type 'NoneType'> is_xhr: <type 'bool'> max_forwards: <type 'NoneType'> method: <type 'str'> GET params: <type 'instance'> path: <type 'str'> / path_info: <type 'str'> / path_info_peek: <type 'instancemethod'> path_info_pop: <type 'instancemethod'> path_qs: <type 'str'> / path_url: <type 'str'> http://localhost:8080/ postvars: <type 'instance'> pragma: <type 'str'> query: <type 'str'> query_string: <type 'str'> queryvars: <type 'instance'> range: <type 'NoneType'> referer: <type 'str'> referrer: <type 'str'> relative_url: <type 'instancemethod'> remote_addr: <type 'str'> 127.0.0.1 remote_user: <type 'NoneType'> remove_conditional_headers: <type 'instancemethod'> request_body_tempfile_limit: <type 'int'> 0 scheme: <type 'str'> http script_name: <type 'str'> server_name: <type 'str'> localhost server_port: <type 'int'> 8080 str_GET: <type 'instance'> str_POST: <class 'webob.multidict.NoVars'> str_cookies: <type 'dict'> {} str_params: <type 'instance'> str_postvars: <class 'webob.multidict.NoVars'> str_queryvars: <type 'instance'> unicode_errors: <type 'str'> ignore uri: <type 'str'> http://localhost:8080/ url: <type 'str'> http://localhost:8080/ urlargs: <type 'tuple'> () urlvars: <type 'dict'> {} user_agent: <type 'str'> Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0