Home > Backend Development > Python Tutorial > Python中使用装饰器和元编程实现结构体类实例

Python中使用装饰器和元编程实现结构体类实例

WBOY
Release: 2016-06-06 11:21:29
Original
1127 people have browsed it

Ruby中有一个很方便的Struct类,用来实现结构体。这样就不用费力的去定义一个完整的类来仅仅用作访问属性。

代码如下:


class Dog end

fred = Dog.new("fred", 5)
printf "name:%s age:%d", fred.name, fred.age
##name:fred age:5


Python3.4中也可以这么干,但写法很累赘。其中包含self.name = name 这种很烦人的写法。

代码如下:


class Dog(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

fred = Dog("fred", 5)
print('name:{name} age:{age}'.format(name=fred.name, age=fred.age))
##name:fred age:5


想到我大Python是无所不能的,有没有一种简化结构体类属性定义的方法呢?答案肯定是有的。在补习了一些Python黑魔法技术后,我想到利用装饰器函数和元编程技术来实现。

代码如下:


def struct(*name):
    """ 装饰器函数
        用途:用于在类定义中,自动设置self.value = value
    """
    def decorator(func):
        def wrapper(*args, **kw):
            for i in range(len(name)):
                setattr(args[0], name[i], args[i+1])
            return func(*args, **kw)
        return wrapper
    return decorator

class Dog(object):
    @struct('name','age')   #黑魔法所在!
    def __init__(self, *all_value):
        pass

fred = Dog("fred", 5)
print('name:{name} age:{age}'.format(name=fred.name, age=fred.age))
##name:fred age:5


要注意的是,这种写法会造成代码结构的不清晰。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template