473,320 Members | 1,861 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,320 software developers and data experts.

just curious

I'm new to Python, and I've noticed the following:
def f(a,b): a+=b def g(a,b): a=a+b p=[1,2,3]
q=[4,5,6]
r=[7,8,9]
s=[10,11,12]
f(p,q)
p [1, 2, 3, 4, 5, 6] g(r,s)
r

[7, 8, 9]

Any deep reason for this, or "just because"? TIA.

Peace,
EJ
Jul 18 '05 #1
3 1570
Elaine Jackson wrote:
Any deep reason for this, or "just because"? TIA.


It's because the augmented assignment operators, like +=, are supposed
to efficiently mutate objects when they have the opportunity (at least
for the builtin classes). So a = a + b always creates a new object, but
a += b might just mutate a preexisting object if that option is
available.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ The great floodgates of the wonder-world swung open.
\__/ Herman Melville
Jul 18 '05 #2

"Elaine Jackson" <el***************@home.com> schrieb im Newsbeitrag
news:rn***********************@news2.calgary.shaw. ca...
I'm new to Python, and I've noticed the following:

(1) Python has a quite strict "call by value" similar to C. This means - as
in C) you can use formal parameters as if they were local variables. (And
there are some useful tricks with that..)
So A = A + B will have no outside impact.

(2) A += B for lists is a shortcut for
A.extend(B)
which means that it changes something A is bound to (or "points to" as you
would say in C). This is somewhat awkward because it sometimes works counter
intuitive as in your case. Just keep in mind: A+=B is *not* A=A+B but
behaves as if in most cases.... ;-)

Kindly
Michael P


def f(a,b): a+=b def g(a,b): a=a+b p=[1,2,3]
q=[4,5,6]
r=[7,8,9]
s=[10,11,12]
f(p,q)
p [1, 2, 3, 4, 5, 6] g(r,s)
r

[7, 8, 9]

Any deep reason for this, or "just because"? TIA.

Peace,
EJ

Jul 18 '05 #3
Hi !

In
def g(a,b):
a=a+b

a new object "a" is create ; but it's a local object, who are not the "a"
global.
In python, variables are "pointer to object".

If you try :
def g(a,b):
global a
a=a+b
You obtain : "name 'a' is global and local"

@-salutations
--
Michel Claveau
Jul 18 '05 #4

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

Similar topics

72
by: Herbert | last post by:
I'm still relativey new to stylesheets, so I'm hoping that the way I'm going about things can be seriously improved upon, i.e . I just haven't undersood something obvious about the 'cascading'...
99
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a...
0
by: theintrepidfox | last post by:
Hi Group It's a rainy day and because of pure boredom have right-clicked a row in EM and selected 'Properties'. Just being curious what the dialog coming up is all about (call me ignorant!)...
13
by: Lumpierbritches | last post by:
I'm curious as to why some questions posted here get results and solutions, while others are answered in a seemingly foreign language and I can't begin to comprehend or understand the answers that...
3
by: CoreyWhite | last post by:
I wrote a program that shows some incredibly curious probabilities. It is a simple game where you guess a number between 0 and 2 that the computer thinks up. What is origonal about this game is...
4
by: Lance | last post by:
Hi all, I'm curious as to the technicalities of why a particular method declaring and calling is faster than the other. The C syntax is: \\\\\ BOOL PathMatchSpec( LPCSTR pszFile, LPCSTR...
1
by: Andrej Nerat | last post by:
Greetings, I was playing a bit with web services and asked myself if I could use P/Invoke to create AVI file in Web service from some images(bitmaps). I could already use P/Invoke to incorporate...
11
by: sterling.mckay | last post by:
Just started to teach myself C C++ programming... I have been very interested with computers for a while now and have a nac or so I thought for how they work ... hardware I am ok with ... I can...
34
by: chandu | last post by:
Hello every body , upto now i saw advantages of C# only,i am curious about what are the disadvantages of C#..(because any programming language should have some positives and negatives..) Thanks...
11
by: Liz | last post by:
anyone have any idea why there have been (at least) 82 VS 2008 post-release messages here on the C# board but only 5 on the VB.NET board; are the VB crowd just not interested?? strikes me as a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.