0

I found out yesterday that it is possible to override operators in python, so after a bit of googling i found out how, but i could not find any way of overloading the "=" sign. There is __set__() but as i understand it, it overloads the sign for attributes in the object, and not for the object itself.

What i want to accomplish is this:

F = Foo(1)
G = Foo(2)
F = G #overloaded =

So is there a way of overloading "=" for an object in python? (and what is that function called)

4
  • No. You cannot overload the assignment operator in Python. See docs.python.org/3/reference/datamodel.html#special-method-names Commented Nov 22, 2017 at 8:42
  • You want to overload = (assignment) not == (equality comparison)? Commented Nov 22, 2017 at 8:44
  • In some cases a property with a setter could do that. But it only works on object attributes not on "free" variables. There are other options you might want to consider: setattr(), a dictionary, the __dict__ attribute… Commented Nov 22, 2017 at 8:47
  • Yes, to basically call a c method when assigning to a different object Commented Nov 22, 2017 at 8:48

1 Answer 1

1

You cannot overload =(assign) operator.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, Thanks for the reply!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.