473,387 Members | 1,510 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,387 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 1027

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.