Python 中的动态对象,如 AS3 中的动态对象

发布于 2024-10-28 00:41:36 字数 752 浏览 2 评论 0原文

在AS3中,我们有一个关键字来定义动态对象:

dynamic class DynamicClass { ... }

因此,我们可以在运行时添加或删除属性。

var dynamicInstance:DynamicClass = new DynamicClass();
// add a property like this...
dynamicInstance.newProperty = 'newValue';
// or this...
dynamicInstance['otherProperty'] = 'otherValue';

我可以访问甚至迭代整个动态属性集合:

for (var name:String in dynamicInstance)
    trace(name, '=', dynamicInstance[name])
// output:
//     newProperty = newValue
//     otherProperty = otherValue

而且我还可以删除这些属性:

// and delete a property like this...
delete dynamicInstance.newProperty;
// or this...
delete dynamicInstance['otherProperty'];

如何在 Python 中做到这一点?

In AS3 we have a keyword to define dynamic objects:

dynamic class DynamicClass { ... }

So, we can add or delete properties in run-time.

var dynamicInstance:DynamicClass = new DynamicClass();
// add a property like this...
dynamicInstance.newProperty = 'newValue';
// or this...
dynamicInstance['otherProperty'] = 'otherValue';

The I can acces or even iterate throught the whole collection of dynamic properties:

for (var name:String in dynamicInstance)
    trace(name, '=', dynamicInstance[name])
// output:
//     newProperty = newValue
//     otherProperty = otherValue

And I also can delete those properties:

// and delete a property like this...
delete dynamicInstance.newProperty;
// or this...
delete dynamicInstance['otherProperty'];

How can this be done in Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-11-04 00:41:36

python中的所有类都是动态的。你可以像这样访问它们

class a:
   pass

>>> a.name="rob"
>>> a.age=45
>>> dir(a)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__',
 '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__',
 '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
 '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name']

>>> a.age
45
>>> a.name
'rob'

>>> setattr(a,"job","fischer")
>>> getattr(a,"name")
'rob'

如果你想更改类的所有实例,你可以轻松编写一个for循环来忽略以__开头的实例

all classes in python are dynamic. you can acces them like this

class a:
   pass

>>> a.name="rob"
>>> a.age=45
>>> dir(a)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__',
 '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__',
 '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
 '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name']

>>> a.age
45
>>> a.name
'rob'

or

>>> setattr(a,"job","fischer")
>>> getattr(a,"name")
'rob'

If you want to change all instances of the class you can easily write a for loop that ignors the instances starting with __

野侃 2024-11-04 00:41:36

一位同事就此给了我建议,我们找到了解决方案:

class Dynamic:
    def __getitem__(self, name):
        return getattr(self, name)

    def __setitem__(self, name, value):
        setattr(self, name, value)

    def __delitem__(self, name):
        delattr(self, name)

    def __iter__(self):
        return self.__dict__.__iter__()

这是一个测试:

dynamicInstance = Dynamic()
dynamicInstance.newProperty = 'newValue'
dynamicInstance['otherProperty'] = 'otherValue'

print [(name, dynamicInstance[name]) for name in dynamicInstance]

del dynamicInstance['newProperty']

print [(name, dynamicInstance[name]) for name in dynamicInstance]

del dynamicInstance.otherProperty

print [(name, dynamicInstance[name]) for name in dynamicInstance]

#output:
# [('otherProperty', 'otherValue'), ('newProperty', 'newValue')]
# [('otherProperty', 'otherValue')]
# []

A co-worker gave me an advice on this and we found a solution for this:

class Dynamic:
    def __getitem__(self, name):
        return getattr(self, name)

    def __setitem__(self, name, value):
        setattr(self, name, value)

    def __delitem__(self, name):
        delattr(self, name)

    def __iter__(self):
        return self.__dict__.__iter__()

Here is a test:

dynamicInstance = Dynamic()
dynamicInstance.newProperty = 'newValue'
dynamicInstance['otherProperty'] = 'otherValue'

print [(name, dynamicInstance[name]) for name in dynamicInstance]

del dynamicInstance['newProperty']

print [(name, dynamicInstance[name]) for name in dynamicInstance]

del dynamicInstance.otherProperty

print [(name, dynamicInstance[name]) for name in dynamicInstance]

#output:
# [('otherProperty', 'otherValue'), ('newProperty', 'newValue')]
# [('otherProperty', 'otherValue')]
# []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文