简体   繁体   English

Python:如何用getattr得到一个object属性属性?

[英]Python: How to get attribute of attribute of an object with getattr?

How do I evaluate我如何评价

a = myobject.id.number

and return None if it myobject is None如果 myobject 为 None,则返回None

with built-in getattr ?内置getattr Maybe getattr(myobject, "id.number", None) ?也许getattr(myobject, "id.number", None)

这应该可以很好地扩展到任何深度:

reduce(lambda obj, attr : getattr(obj, attr, None), ("id","num"), myobject)
getattr(getattr(myobject, "id", None), "number", None)

应该管用。

Here's a one-liner 这是一个单行

a = myobject is not None and myobject.id.number or None

It doesn't check whether id is None, but that wasn't part of the original question. 它不会检查id是否为None,但这不是原始问题的一部分。

A slightly over generic solution keeping in view all members: 一个稍微过于通用的解决方案,让所有成

if myobject and myobject.id and myobject.id.number:
    a = myobject.id.number
else:
    a = None
return myobject.id.number if myobject else None

my favorites are 我最喜欢的是

from functools import reduce
try:
  a = reduce(getattr, ("id", "number"), myobject)
except AttributeError:
  a = None

or 要么

from operator import attrgetter
try:
  a = attrgetter('id.number')(myobject)
except AttributeError:
  a = None

I use the following function which does it for any level.我使用以下 function,它适用于任何级别。

def Resolve(object, attribute:str):
  """
  Resolve attribute of an object.
  """
  attribute_list = attribute.split(".")
  obj = object
  try:
    for att in attribute_list:
      obj = getattr(obj, att)
  except AttributeError:
    obj = None
    
  return obj

To use it you write:要使用它你写:

a = Resolve(myobject, 'id.number')

The code simply splits the string using the the period character and loops through the attributes.该代码只是使用句点字符拆分字符串并循环遍历属性。 If you had another level say `myobject.id.number.another' you would use:如果你有另一个级别说“myobject.id.number.another”你会使用:

a = Resolve(myobject, 'id.number.another')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从python中的对象获取getattr属性 - get attribute with getattr from a object in python 如何在python中使用getattr获取Django模型的外键对象的属性? - How to get attribute of a django model's foreign key object using getattr in python? Python - Kivy:AttributeError:'super'对象在尝试获取self.ids时没有属性'__getattr__' - Python - Kivy: AttributeError: 'super' object has no attribute '__getattr__' when trying to get self.ids Python getattr() 以另一个属性作为默认值 - Python getattr() with another attribute as the default Python Kivy 返回 'AttributeError: 'super' object 没有属性 '__getattr__'' - Python Kivy returning 'AttributeError: 'super' object has no attribute '__getattr__'' 'super'对象在python3中没有属性'__getattr__' - 'super' object has no attribute '__getattr__' in python3 AttributeError: 'super' 对象在 python 中没有属性 '__getattr__' - AttributeError: 'super' object has no attribute '__getattr__' in python python kivy AttributeError:“超级”对象没有属性“ __getattr__” - python kivy AttributeError: 'super' object has no attribute '__getattr__' 如何获取魔术__getattr__中的实例属性? - How to get instance attribute inside magic __getattr__? AttributeError:“超级”对象没有属性“ __getattr__” - AttributeError: 'super' object has no attribute '__getattr__'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM