python中getattr函数使用方法 getattr实现工厂模式
来源:诚信在线    发布时间:2017-06-28 19:09:25

  result = func(args)

  getattr(object, name[, default]) -> value

  [code]

  func(args)

  为了加深对于getattr函数的懂得 转载一篇英文的申明

  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:

  except AttributeError:

  这个例子外能够凭据传入output函数的format参数的分歧 往挪用statsout模块差别的办法(用格式化字符串完成output_%s)

  Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.

  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:

  doc = getattr(len, "__doc__")

  exist; without it, an exception is raised in that case.

  if func:

  else:

  望了高函数自身的doc

  func = getattr(obj, "method", None)

  result = obj.method(args)

  value = getattr(obj, "attribute")

  or, in one line:

  func = getattr(obj, "method")

  func = getattr(obj, "method", None)

  你能够感兴趣的文章:JS面向工具根底解说(工场形式、结构函数形式、原型形式、混杂形式、静态原型形式)深刻c#工场形式的详解PHP 工场形式使用方法javascript 形式计划之工场形式学习心得C++设想形式之简朴工场形式实例JavaScript 形式之工场形式(Factory)使用先容PHP最经常使用的二种设想形式工场形式战双例形式引见asp.net 简略工场形式战工场法子形式之叙述php 设想形式之 工场形式.NET容易工场形式解说

  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.

  If the attribute exists, the corresponding value is returned. If the attribute does not exist, you get an AttributeError exception instead.

  [code]

  def output(data, format="text"):                          

  object.name

  path = getattr(sys, "path")

  result = getattr(obj, "method")(args)

  诠释的很笼统 告诉我这个函数的感化至关因而

  Here's a variation, which checks that the attribute is indeed a callable object before calling it.

  result = func(args)

  func = getattr(obj, "method")

  import statsout

  The getattr function uses the same lookup rules as ordinary attribute access, and you can use it both with ordinary attributes and methods:

  复制代码 代码以下:

  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(args)

  When a default argument is given, it is returned when the attribute doesn't

  试了一下getattr(object,name)确切以及object.name是同样的功效.只不过此处能够把name作为一个变量往解决书上的例子很好的道明白这个函数的功能,利用getattr能够轻松完成工场形式。

  output_function = getattr(statsout, "output_%s" %format)

  if callable(func):

  返回的是这个办法的工具 便能够间接利用了 假如要增添新的格局 只须要正在模块外写入新的方式函数 正在挪用output函数时运用新的参数便能够应用差别的体例输出

  确切很不便

  return output_function(data)

  value = obj.attribute

  ... deal with missing method ...

  例:一个模块支撑html、text、xml等花样的打印,按照传入的formate参数的分歧,挪用差别的函数完成多少种款式的输出


诚信在线 除注明原创以外,其余均来自互联网以及微信朋友圈,如有侵权请联系站长立即删除!
文章地址:http://www.my517517.com/DP/20170628327.html



上一篇:基于hashlib模块 下一篇:Python 稀疏矩阵