python Python中的简单类型检查对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python中的简单类型检查对象相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python

__author__ = "Luca Giacomel"
__license__ = "Public Domain"

def generate_setter(field):
    def setter(self, value):
        field = setter.__name__[4:]
        if isinstance(value, self.enforced_types[field]):
            setattr(self, "_%s" % field, value)
        else:
            raise TypeError("%s object cannot have property %s with value \"%s\" (type: %s - it must be %s)" % (
                self.__class__,
                field,
                value,
                type(value),
                self.enforced_types[field]))

    setter.__name__ = "set_%s" % field
    return setter


def generate_getter(field):
    def getter(self):
        field = getter.__name__[4:]
        if not hasattr(self, "_%s" % field):
            raise ValueError("property %s of instance %s of object %s is undefined" %
                             (field, self.__repr__(), self.__class__))
        return getattr(self, "_%s" % field)

    getter.__name__ = "set_%s" % field
    return getter


def entity_factory(entity_type, **kwargs):
    props = dict()
    props['enforced_types'] = kwargs
    for obj, _ in kwargs.items():
        props[obj] = property(generate_getter(obj), generate_setter(obj))

    def __init__(self, **kwargs):
        for attr, tpe in kwargs.items():
            if attr in props['enforced_types'] and isinstance(attr, props['enforced_types'][attr]):
                setattr(self, "_%s" % attr, tpe)

    props['__init__'] = __init__

    cls = type(
        entity_type,
        (object,),
        props
    )
    return cls
#!/usr/bin/env python

from type_checked_entities import entity_factory

Giraffe = entity_factory(  # let's define what is a giraffe!
    "giraffe",
    name=str,  # my name is a string
    age=float,  # my age is an int
    eats=object,  # I eat pretty much everything.
)

Giraffe.say_hi = lambda self: "the giraffe {} says hi!".format(self.name)  # example property of a giraffe

Grass = entity_factory(
    "grass",
    color='green'
)
grass = Grass()

giraffe_1 = Giraffe(
    name="Gira",
    age=21,
    eats=grass
)

giraffe_2 = Giraffe()

print giraffe_1.say_hi()  # the giraffe Gira says hi!

try:
    print giraffe_2.say_hi()  # explicitly fails, giraffe_2.name needed for the method is still undefined
except ValueError as error:
    print error

giraffe_2.name = "rhino"  # whoa, didn't see this coming
giraffe_2.say_hi()

try:
    giraffe_2.age = 11.5
except TypeError as error:  # 11.5 is not an valid int age
    print error
    giraffe_2.age = 11

以上是关于python Python中的简单类型检查对象的主要内容,如果未能解决你的问题,请参考以下文章

查找 Python 对象具都有哪些方法

Python:检查对象的类型

Python的自省机制

如何检查我的 python 对象是不是为数字? [复制]

Python检查类的实例

如何在 Python 中检查对象的类型? [复制]