Python:函数名称的字符串,它是如何工作的?

时间:2018-02-27 18:46:07

标签: python python-3.x

在pandas中,您可以通过

获取数据框中列的唯一值
#List unique values in the df['name'] column
df.name.unique()

Python如何将函数名称转换为字符串参数' name'?

1 个答案:

答案 0 :(得分:2)

数据框覆盖__getattr__以挂钩属性解析。这允许从df.anything返回任意对象。该实例在self._internal_names_set中维护一个“真实”属性名称的内部列表,并在self._info_axis中维护一个列名列表。

def __getattr__(self, name):
    """After regular attribute access, try looking up the name
    This allows simpler access to columns for interactive use.
    """

    # Note: obj.x will always call obj.__getattribute__('x') prior to
    # calling obj.__getattr__('x').

    if (name in self._internal_names_set or name in self._metadata or
            name in self._accessors):
        return object.__getattribute__(self, name)
    else:
        if name in self._info_axis:
            return self[name]
        return object.__getattribute__(self, name)

来源是here