如何在Python中模拟赋值运算符重载?

发布于 2024-11-08 02:52:22 字数 335 浏览 0 评论 0原文

如何在 Python 中模拟赋值运算符重载?例如...

class Example(object):

    name = String()
    age = Integer()

    def __init__(self,myname,myage):
        self.name.value = myname
        self.age.value = myage

除了执行 self.name.value = name 之外,如何模拟赋值运算符的重载,以便在执行 self.name = myname 时将 myname 分配给 self.name.value ?

How can you emulate assignment operator overloading in Python? For example...

class Example(object):

    name = String()
    age = Integer()

    def __init__(self,myname,myage):
        self.name.value = myname
        self.age.value = myage

Rather than doing self.name.value = name, how can you emulate overloading of the assignment operator so that myname is assigned to self.name.value when you do self.name = myname?

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

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

发布评论

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

评论(4

鹿港巷口少年归 2024-11-15 02:52:22

在这种非常特殊的情况下,在属性分配中,您可以使用 descriptor< /代码>。事实上,我怀疑在您使用的示例中,IntegerString 实际上是描述符。

除了使用预制描述符之外,使用描述符的最简单方法是使用 property()< /代码>。这是一个简短的例子:

>>> class Foo(object):
        @property
        def bar(self):
            print 'bar'
            return 'bar'
        @bar.setter
        def bar(self, value):
            print 'bar =', value


>>> afoo = Foo()
>>> afoo.bar
bar
'bar'
>>> afoo.bar = 'baz'
bar = baz
>>> 

In this very special case, in attribute assignment, you can use a descriptor. In fact, I suspect that in the example you are using, Integer and String are actually descriptors.

Aside from using premade descriptors, the easiest way to use descriptors is with property(). here's a brief example:

>>> class Foo(object):
        @property
        def bar(self):
            print 'bar'
            return 'bar'
        @bar.setter
        def bar(self, value):
            print 'bar =', value


>>> afoo = Foo()
>>> afoo.bar
bar
'bar'
>>> afoo.bar = 'baz'
bar = baz
>>> 
将军与妓 2024-11-15 02:52:22

你不能在 python 中重载赋值运算符,但是通过一些巧妙的魔术方法重载,你可以通过重载 rshift 魔术方法来获得 A <<= B+C,这是有关 python 魔术的综合指南方法请参阅

You cannot overload the assignment operator in python however with some clever overloading of magic methods you can get to A <<= B+C by overloading the rshift magic method, for a comprehensive guide on pythons magic methods see this.

百变从容 2024-11-15 02:52:22

您不能超载分配。这不是运营商。您最好只在对象构造函数中构造值。

class Example(object):

    def __init__(self,myname, myage):
        self.name = String(myname)
        self.age = Integer(myage)

但是在这种情况下,我不明白为什么不能只使用内置的 strint

You can't overload assignment. It's not an operator. You would be better off here just constructing the value in the object constructor.

class Example(object):

    def __init__(self,myname, myage):
        self.name = String(myname)
        self.age = Integer(myage)

However in this case I don't see why you can't just use the built-in str and int.

花开雨落又逢春i 2024-11-15 02:52:22

我最终创建了一个名为 ModelMeta 的模型元类,用于注册类型化属性。

请参阅http://github.com/espeed/bulbs/blob/master/ 在这种情况下,类型化属性是图形数据库“属性” ,

它们都是 Property 类的子类。

请参阅 https://github.com/espeed/bulbs/blob/master/ bulbs/property.py

这是一个示例模型声明:

# people.py

from bulbs.model import Node, Relationship
from bulbs.property import String, Integer, DateTime
from bulbs.utils import current_datetime

class Person(Node):

    element_type = "person"

    name = String(nullable=False)
    age = Integer()


class Knows(Relationship):

    label = "knows"

    created = DateTime(default=current_datetime, nullable=False)

示例用法:

>>> from people import Person
>>> from bulbs.neo4jserver import Graph
>>> g = Graph()

# Add a "people" proxy to the Graph object for the Person model:
>>> g.add_proxy("people", Person)

# Use it to create a Person node, which also saves it in the database:
>>> james = g.people.create(name="James")
>>> james.eid
3
>>> james.name
'James'

# Get the node (again) from the database by its element ID:
>>> james = g.people.get(james.eid)

# Update the node and save it in the database:
>>> james.age = 34
>>> james.save()

# Lookup people using the Person model's primary index:
>>> nodes = g.people.index.lookup(name="James")

请参阅...

I ended up creating a Model metaclass called ModelMeta that registers the typed attributes.

See http://github.com/espeed/bulbs/blob/master/bulbs/model.py

In this case, the typed attributes are graph-database "properties", which are all subclasses of the Property class.

See https://github.com/espeed/bulbs/blob/master/bulbs/property.py

Here's an example Model declaration:

# people.py

from bulbs.model import Node, Relationship
from bulbs.property import String, Integer, DateTime
from bulbs.utils import current_datetime

class Person(Node):

    element_type = "person"

    name = String(nullable=False)
    age = Integer()


class Knows(Relationship):

    label = "knows"

    created = DateTime(default=current_datetime, nullable=False)

Example usage:

>>> from people import Person
>>> from bulbs.neo4jserver import Graph
>>> g = Graph()

# Add a "people" proxy to the Graph object for the Person model:
>>> g.add_proxy("people", Person)

# Use it to create a Person node, which also saves it in the database:
>>> james = g.people.create(name="James")
>>> james.eid
3
>>> james.name
'James'

# Get the node (again) from the database by its element ID:
>>> james = g.people.get(james.eid)

# Update the node and save it in the database:
>>> james.age = 34
>>> james.save()

# Lookup people using the Person model's primary index:
>>> nodes = g.people.index.lookup(name="James")

See...

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