473,796 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1587
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
4867
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' nature of the coding, which I believe concerns the way attributes relate to one another when 'nested'... I think I can illustrate the nature of the beast with this example, using just two text styles:
99
6249
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 small or mid-sized business. http://groups-beta.google.com/group/microsoft.public.msdn.general/browse_thread/thread/9d7e8f9a00c1c7da/459ca99eb0e7c328?q=%22Proposed+MSDN+subscription+changes%22&rnum=1#459ca99eb0e7c328 Damn! To be that...
0
934
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!) and where I can find more information (Keyword?)in BOL? One thing I wonder however and maybe someone can answer this question is that this dialog suggests that a table is nothing more than a query
13
1411
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 are given. Is there a place to learn this foreign language to be able to better communicate questions, as to derive the solution that the poster is attempting to receive? If so, where please? I have a program that I'm trying to track animal...
3
1599
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 the computer tells you before hand if you got the number right and you can decide if you want to play or not. The computer keeps track of all your wins, all your losses, and as is possible all of your didn't wins but didn't loses. What is...
4
1554
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 pszSpec );
1
1202
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 OpenGL to web services (to create images), and I am curious if I could do the same with AVI files or streams? Perhaps someone already tried something like that or knows if it can be done.
11
1649
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 understand and tear it down and rebuild it ... chips boards I/O etc etc ... programing is a different beast ... So I am fooling around with C and compile a small .exe file with Miracle C ... simple thing that said Hi my name is etc etc ... but I...
34
8188
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 Chandu
11
1364
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 significant difference but I'm not sure what to make of it ....
1
10182
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
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
7552
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
6793
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();...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
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.