473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Re: adding in-place operator to Python

Arash Arfaee wrote:
Hi All,

Is there anyway to add new in-place operator to Python? Or is there any
way to redefine internal in-place operators?
Python does not have 'in-place operators'. It has 'augmented assignment
statements' that combines a binary operation with an assignment. *If*
the target being rebound is mutable, *then* (and only then) the
operation can be and is recommended to be done 'in-place'. User-defined
mutable classes (as most are) can implement in-place behavior with
__ixxx__ methods. But for the reason given below, __ixxx__ methods
should supplement and not replace direct mutation methods.

Correct terminology is important for understanding what augmented
assigments do and what they are basically about.

First, most augmented assignments target immutables, in particular,
numbers and strings, which do not have __ixxx__ methods. So the
operation is *not* done in-place. The only difference from separately
indicating the assignment and operation is that the programmer writes
the target expression just once and the interpreter evaluates the target
expression just once instead of each repeating themselves. (And
consequently, any side-effects of that evaluation happen just once
instead of twice.) The same __xxx__ or __rxxx__ method is used in
either case. This non-repetition is the reason for augmented
assigments. The optional in-place optimization for mutables is
secondary. It was debated and could have been left out.

Second, all augmented assignments perform an assignment, even if the
operation is done in place. However, if a mutable such as a list is
accessed as a member of an immutable collection such as a tuple,
mutation is possible, but rebinding is not. So the mutation is done and
then an exception is raised. To avoid the exception, directly call a
mutation method such as list.extend.

Terry Jan Reedy

Sep 23 '08 #1
0 1028

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dada | last post by:
I have many php files and I would like to simulate low speed connectivity by adding sleep(5); statement to all the php pages. Instead of adding this statement manually to all the php files is...
0
by: John Smith | last post by:
When the code below runs, I get the error: "cannot convert from 'System.Web.UI.WebControls.BoundField' to 'System.Web.UI.WebControls.DataGridColumn'" Is adding a BoundField different than adding...
10
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
3
by: Gabriele *darkbard* Farina | last post by:
Hi, there is a way to add methods to an object dynamically? I need to do something like this. I remember python allowed this ... class A(object): def do(s, m): print m @staticmethod def...
2
by: Chris Cobb | last post by:
I have a table that currently contains 718000 rows. I wish to add a column to the table. Adding this column anywhere other than the end of the table requires exporting data, a drop and recreate,...
3
by: van6 | last post by:
code frag: int main() { float fval1 = 1.0f; float fval2 = 2.0f; double dsum =0.0; // here is the point ; // Are fval1 and fval2 both promoted to type of double prior to adding
9
by: Kadett | last post by:
Hi all, I have following problem: I'm creating a ListView (Details) control at run-time and filling it with some records (let's say 10 000). This operation seems to be quite fast, but when I call...
1
by: trint | last post by:
My code is just adding to the first row instead of adding to the next: TextReader tr = new StreamReader(@"C:\Program Files \webResponse3\websiteLog.log"); string lineTime; //string namesTime =...
1
by: tonylove42 | last post by:
HI, I have probelm adding, updating and deleteing a record. Here is my code for adding . The error I get is: Operation must use an updateable query. Line 135: MyCmd.ExecuteNonQuery() ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.