How to Emulate Assignment Operator Overloading in Python?

懵懂的女人 提交于 2019-12-18 09:04:49

问题


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?


回答1:


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
>>> 



回答2:


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.




回答3:


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.




回答4:


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...

  • Bulbs Model API: http://bulbflow.com/docs/api/bulbs/model/
  • Bulbs Model Quickstart: http://bulbflow.com/quickstart/#models


来源:https://stackoverflow.com/questions/6012763/how-to-emulate-assignment-operator-overloading-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!