0

Is there an alternative to = sign in python? For example, += can be substituted by __iadd__. In R, both <- and = can be used. Is there such an alternative for =?

Here is the reason for my asking:

File "<ipython-input-14-6ea5b5c012fa>", 
line 37 print("Downloading:", year, "Q%d" % quarter, end = " | ") 
                                                         ^ 
SyntaxError: invalid syntax

I could not find the same question in Stack.

Jim O.
  • 1,091
  • 12
  • 31
  • "For example, += can be substituted by `__iadd__`" - no, it can't, because `__iadd__` is only sometimes responsible for the job, and because even when `+=` calls `__iadd__`, the actual assignment is still a part of `+=`, and not something `__iadd__` can handle. – user2357112 Sep 28 '18 at 02:06
  • What's the goal you are trying to accomplish? – blhsing Sep 28 '18 at 02:10
  • I have a function that I need to use for my assignment, but it gives me this error: File "", line 37 print("Downloading:", year, "Q%d" % quarter, end = " | ") ^ SyntaxError: invalid syntax .. It's hard to show it using a comment, but the error is pointing towards the equal sign. – Jim O. Sep 28 '18 at 02:16
  • Sounds like you're on Python 2 when you really need Python 3. (Also, that's a keyword argument, not assignment. Completely different thing, just with the same symbol.) – user2357112 Sep 28 '18 at 02:25
  • @Keith This should not be a duplicate to the linked question because of the OP's stated goal in his comment. The right duplicate would point him to one that shows him how to use `from __future__ import print_function` instead. – blhsing Sep 28 '18 at 02:48
  • @blhsing hm, there's no code so it's hard to tell. His actual question seems to ask about an operator overload for `=`, in the general case. – Keith Sep 28 '18 at 02:53
  • @Keith Well the error message he included in his comment does contain code so it's quite clear. It would be nice if he updates his question to include that error though. – blhsing Sep 28 '18 at 02:55
  • @blhsing I see. His comment is a totally different thing. Going by just the title and original question it is a duplicate of that. Making it a duplicate of something else based on a comment could be confusing to someone else searching. – Keith Sep 28 '18 at 02:58
  • @Keith Yes I agree with that. – blhsing Sep 28 '18 at 02:59
  • Thank you for vesting your time to my question. I appreciate it very much. I have updated my question as some suggested. – Jim O. Sep 28 '18 at 22:00

1 Answers1

0

No. The equal assigns an object to a name (or label). It does not call any special methods.

Keith
  • 42,110
  • 11
  • 57
  • 76
  • Not in the global namespace, but its possible within a module. See details here: https://stackoverflow.com/questions/11024646/is-it-possible-to-overload-python-assignment – ag415 Sep 28 '18 at 02:06
  • True, thanks for the pointer, @ag415. That's setting an attribute on an object. The original question seems to imply a general, global assignment. – Keith Sep 28 '18 at 02:11