473,671 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1032

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

Similar topics

4
1825
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 there a way to globally give this statement or any other way to add delay to all pages
0
1086
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 a BoundColumn? I cannot find any websites that say otherwise. DataGrid gvViewAll = new DataGrid(); gvViewAll.AutoGenerateColumns = false;
10
2678
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
2286
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 .... py> C().spam(3) spam spam spam
3
1921
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 init(obj): obj.do = A.do
2
1977
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, then a re-import of data. Is there any disadvantage to adding this column to the end of the table. Will this fragment the table. Is a reorg needed? Thanks, - CBC
3
1614
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
4480
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 Controls.Add(list), where list is my ListView control, then program hangs for minute. It seems like this time depends on number of records added to control. So probably when adding control to my form, those records are actualy added at this time...
1
1745
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 = new string; int i = 0; while ((lineTime = tr.ReadLine()) != null) {
1
1215
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() I get same error for update and delete. here is my code for adding: MySQL = "INSERT INTO Property " & _ "(PropAddress, PropCity, PropSuburb, PropType, " & _
0
8472
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8909
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8667
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.