阅读prettytable 一些代码、get、set 检查参数
阅读代码是因为我发现官方教程里的代码在本地不能用,所以就通过”查看定义“转到了源代码里。
通过阅读源代码,查看方法内是否有教程中所说的方法名和参数名,然后再通过”查看引用“来试图了解函数的流程,如果没有头绪,就通过查找关键字,最后发现我下的这个版本里并没有教程中说给的方法
此外,再阅读代码的过程中作者 定义的一种参数是否规范的的验证机制
def _validate_option(self, option, val):
if option in ("field_names"):
self._validate_field_names(val)
elif option in ("start", "end", "max_width", "padding_width",
"left_padding_width", "right_padding_width", "format"):
self._validate_nonnegative_int(option, val)
elif option in ("sortby"):
self._validate_field_name(option, val)
elif option in ("sort_key"):
self._validate_function(option, val)
elif option in ("hrules"):
self._validate_hrules(option, val)
elif option in ("vrules"):
self._validate_vrules(option, val)
elif option in ("fields"):
self._validate_all_field_names(option, val)
elif option in ("header", "border", "reversesort", "xhtml", "print_empty"):
self._validate_true_or_false(option, val)
elif option in ("header_style"):
self._validate_header_style(val)
----snip----
def _validate_header_style(self, val):
try:
assert val in ("cap", "title", "upper", "lower", None)
except AssertionError:
raise Exception("Invalid header style, use cap, title, upper, lower or None!")
def _get_header(self):
"""Controls printing of table header with field names
Arguments:
header - print a header showing field names (True or False)"""
return self._header
def _set_header(self, val):
self._validate_option("header", val)
self._header = val
#从这里开始,
header = property(_get_header, _set_header)
1、get和set方法能在保证变量私有性的同时,又能让外部代码访问和修改变量,这和直接修改还是有区别的,因为在set方法中可以对变量进行检查
2、检查参数的方式

浙公网安备 33010602011771号