Python:如何确定类中属性的类型?

问题描述 投票:-1回答:1

我定义了以下类继承自其他一些类。 Goblin是我正在扩展的Python依赖包。

class AnnotatedVertexProperty(goblin.VertexProperty):
    notes = goblin.Property(goblin.String)
    datetime = goblin.Property(DateTime)

class KeyProperty(goblin.Property):
    def __init__(self, data_type, *, db_name=None, default=None, db_name_factory=None):
        super().__init__(data_type, default=None, db_name=None, db_name_factory=None)

class TypedVertex(goblin.Vertex):
    def __init__(self):
        self.vertex_type = self.__class__.__name__.lower()
        super().__init__()

class TypedEdge(goblin.Edge):
    def __init__(self):
        self.edge_type = self.__class__.__name__.lower()
        super().__init__()

class Airport(TypedVertex):
    #label
    type = goblin.Property(goblin.String)
    airport_code = KeyProperty(goblin.String)
    airport_city = KeyProperty(goblin.String)
    airport_name = goblin.Property(goblin.String)
    airport_region = goblin.Property(goblin.String)
    airport_runways = goblin.Property(goblin.Integer)
    airport_longest_runway = goblin.Property(goblin.Integer)
    airport_elev = goblin.Property(goblin.Integer)
    airport_country = goblin.Property(goblin.String)
    airport_lat = goblin.Property(goblin.Float)
    airport_long = goblin.Property(goblin.Float)

在运行时,我需要迭代抛出每个属性并能够确定其类类型(keyProperty或goblin.Property)我还需要能够确定该值是否为字符串,整数等...

在实例化期间,我创建一个机场对象并将值设置如下:

lhr = Airport()
lhr.airport_code = 'LHR'
print (lhr.airport_code.__class__.mro())
lhr.airport_city = 'London'
lhr.airport_name = 'London Heathrow International Airport'
lhr.airport_region = 'UK-EN'
lhr.airport_runways = 3
lhr.airport_longest_runway = 12395
lhr.airport_elev = 1026
lhr.airport_country = 'UK'
lhr.airport_lat = 33.6366996765137
lhr.airport_long = -84.4281005859375 

但是当我在调试它时检查对象时,我得到的只是属性名称,定义为字符串和值,定义为字符串,整数等...如何检查每个属性的对象类型?有关如何处理此问题的任何帮助或建议?

python python-3.x multiple-inheritance
1个回答
0
投票

我想出了我在找什么。我不得不调用element.class.dict.items():我可以得到一个包含所有属性,映射等的字典......

© www.soinside.com 2019 - 2024. All rights reserved.