可通过属性名称或索引选项访问的结构 [英] Structure accessible by attribute name or index options

查看:81
本文介绍了可通过属性名称或索引选项访问的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python还是很陌生,并试图弄清楚如何创建一个对象,该对象具有可通过属性名称或索引访问的值。例如,os.stat()返回stat_result或pwd.getpwnam()返回struct_passwd的方式。

I am very new to Python, and trying to figure out how to create an object that has values that are accessible either by attribute name, or by index. For example, the way os.stat() returns a stat_result or pwd.getpwnam() returns a struct_passwd.

在试图弄清楚这一点时,我只遇到了上述类型的C实现。 Python没什么特别的。创建此类对象的Python本机方式是什么?

In trying to figure it out, I've only come across C implementations of the above types. Nothing specifically in Python. What is the Python native way to create this kind of object?

对于此问题已经被广泛报道,我深表歉意。在寻找答案时,我必须缺少一些基本的概念,使我无法找到答案。

I apologize if this has been widely covered already. In searching for an answer, I must be missing some fundamental concept that is excluding me from finding an answer.

推荐答案

您不能使用与os.stat()的结果对象和其他对象相同的实现。但是,Python 2.6具有一个新的工厂函数,该函数创建了一个类似的数据类型,称为tuple。命名元组是一个元组,其插槽也可以通过名称来寻址。根据文档,命名元组不应该需要比常规元组更多的内存,因为它们没有每个实例的字典。工厂函数签名是:

You can't use the same implementation as the result object of os.stat() and others. However Python 2.6 has a new factory function that creates a similar datatype called named tuple. A named tuple is a tuple whose slots can also be addressed by name. The named tuple should not require any more memory, according to the documentation, than a regular tuple, since they don't have a per instance dictionary. The factory function signature is:

collections.namedtuple(typename, field_names[, verbose])  

第一个参数指定新类型的名称,第二个参数是包含字段名称的字符串(用空格或逗号分隔),最后,如果verbose为true,则工厂函数还将打印生成的类。

The first argument specifies the name of the new type, the second argument is a string (space or comma separated) containing the field names and, finally, if verbose is true, the factory function will also print the class generated.

示例

假设您有一个包含用户名和密码的元组。要访问用户名,您将在零位置获取该项目,并在第一位置访问密码:

Suppose you have a tuple containing a username and password. To access the username you get the item at position zero and the password is accessed at position one:

credential = ('joeuser', 'secret123')  
print 'Username:', credential[0]  
print 'Password:', credential[1]  

这段代码没有什么错,但是元组不是自记录的。您必须找到并阅读有关元组中字段位置的文档。这是命名元组可以进行救援的地方。我们可以重新编码前面的示例,如下所示:

There's nothing wrong with this code but the tuple isn't self-documenting. You have to find and read the documentation about the positioning of the fields in the tuple. This is where named tuple can come to the rescue. We can recode the previous example as follows:

import collections  
# Create a new sub-tuple named Credential  
Credential = collections.namedtuple('Credential', 'username, password')  

credential = Credential(username='joeuser', password='secret123')  

print 'Username:', credential.username  
print 'Password:', credential.password  

如果您对新创建的凭据类型的代码外观感兴趣,可以在创建类型时在参数列表中添加verbose = True,在这种情况下,我们将获得以下输出:

If you are interested of what the code looks like for the newly created Credential-type you can add verbose=True to the argument list when creating the type, in this particular case we get the following output:

import collections  
Credential = collections.namedtuple('Credential', 'username, password', verbose=True)  

class Credential(tuple):                                       
    'Credential(username, password)'                       

    __slots__ = ()   

    _fields = ('username', 'password')   

    def __new__(_cls, username, password):  
        return _tuple.__new__(_cls, (username, password))   

    @classmethod  
    def _make(cls, iterable, new=tuple.__new__, len=len):  
        'Make a new Credential object from a sequence or iterable'  
        result = new(cls, iterable)                                 
        if len(result) != 2:                                        
            raise TypeError('Expected 2 arguments, got %d' % len(result))  
        return result  

    def __repr__(self):  
        return 'Credential(username=%r, password=%r)' % self  

    def _asdict(t):  
        'Return a new dict which maps field names to their values'  
        return {'username': t[0], 'password': t[1]}  

    def _replace(_self, **kwds):  
        'Return a new Credential object replacing specified fields with new values'  
        result = _self._make(map(kwds.pop, ('username', 'password'), _self))  
        if kwds:  
            raise ValueError('Got unexpected field names: %r' % kwds.keys())  
        return result  

    def __getnewargs__(self):  
        return tuple(self)  

    username = _property(_itemgetter(0))  
    password = _property(_itemgetter(1))  

命名元组不仅提供对字段的访问按名称但还包含辅助函数,例如_make()函数,该函数可帮助从序列或可迭代对象创建凭据实例。例如:

The named tuple doesn't only provide access to fields by name but also contains helper functions such as the _make() function which helps creating an Credential instance from a sequence or iterable. For example:

cred_tuple = ('joeuser', 'secret123')  
credential = Credential._make(cred_tuple) 

namedtuple的python库文档提供了更多信息和代码示例,因此建议您看一眼。

The python library documentation for namedtuple has more information and code examples, so I suggest that you take a peek.

这篇关于可通过属性名称或索引选项访问的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆