tag:blogger.com,1999:blog-9266717.post111668572037585196..comments2023-05-22T10:01:23.167-03:00Comments on Only Python: N = N + 1André Robergehttp://www.blogger.com/profile/08131391818998844540noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-9266717.post-1117189529822461842005-05-27T07:25:00.000-03:002005-05-27T07:25:00.000-03:00Laurent: For the record, it is "Bruce" that said t...Laurent: <BR/><BR/>For the record, it is "Bruce" that said that <I>Scalar assignments ... are assigned by value. ...</I><BR/><BR/>I agree with your statement that <BR/><I>the assignmenet a=b is the same whatever is b.<BR/>In python all asignements are reference.</I><BR/><BR/>In fact, I wrote, in reply to Bruce:<BR/><I><BR/>It does not matter whether the object on the right hand side is a scalar, a list or some more complicated object.<BR/>...<BR/>On the right hand side of the = sign is an object.<BR/>On the left hand side is a "reference" to that object... </I><BR/><BR/>Note that I tried to explain things in the simplest possible way (without even touching concepts like mutability), as if I were trying to explain what "=" means to someone who has <B>never</B> done any programming before. Perhaps I oversimplified?André Robergehttps://www.blogger.com/profile/08131391818998844540noreply@blogger.comtag:blogger.com,1999:blog-9266717.post-1117188347372084522005-05-27T07:05:00.000-03:002005-05-27T07:05:00.000-03:00Andre saidScalar assignments (like:une_fois = once...Andre said<BR/><I><BR/>Scalar assignments (like:<BR/>une_fois = once<BR/>) are assignments by value <BR/><BR/>List assignments (like:<BR/>epicerie = groceries<BR/>) are assignments by reference <BR/></I><BR/><BR/>No, I don't agree with you!<BR/>the assignmenet a=b is the same whatever is b.<BR/>In python all asignements are reference.<BR/>a is a new name for the object (it now has 2 names a and b).<BR/>But there are some object that are mutable (like list, dict ..) and othere that are not.<BR/>so when you did a=1, you could chnage the inner value of 1 (int are not mutable)!<BR/>A list is mutable, a=[], you can do a.append(1)...<BR/><BR/>When you did a=b this is the same object (you can see it with "a is b") whatever the type of object.<BR/>Every time it is a reference assignement, just mean mutable / not mutable object !<BR/><BR/>Morever you write:<BR/><I><BR/>'une_fois' takes the value that 'once' has at the time of the assignment and remains unaffected by subsequent assignments to 'once'?<BR/></I><BR/>It is exacly the same for list !<BR/>a=[]<BR/>b=a<BR/>a.append(1)<BR/>a=[]<BR/>b is [1] and a is []! so no affectation of the value of b by assignement of a.<BR/><BR/>I hope it will clear everithing. But it is really important to undersand python as name for object (no assignement like in other language) as you can see withn the NameError exception !Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9266717.post-1117182071482294132005-05-27T05:21:00.000-03:002005-05-27T05:21:00.000-03:00When i learned programming i used to trip over thi...When i learned programming i used to trip over this all the time, until i realised that <BR/><BR/>"=" is not "equals"<BR/><BR/>"=" is "gets"<BR/><BR/>eg. N=N+1, when i read this i say to myself "N gets N plus 1"Simonhttps://www.blogger.com/profile/05433884038328510274noreply@blogger.comtag:blogger.com,1999:blog-9266717.post-1117131831325947982005-05-26T15:23:00.000-03:002005-05-26T15:23:00.000-03:00Bruce:First: I am just a hobbyist myself - so take...Bruce:<BR/><BR/>First: I am just a hobbyist myself - so take my assertions with a grain of salt :-)<BR/><BR/>In Python, *everything* is an object.<BR/>[As an experiment, try dir(a) to see the methods associated with instance or object "a". For example, dir(1).]<BR/><BR/>Whereas other language make a difference between assignments "by value" or "by reference", in Python there is essentially (afaik) only one type of assignment.<BR/><BR/>On the right hand side of the = sign is an object. <BR/>On the left hand side is a "reference" to that object. I prefer to use the word "synonym" rather than "reference", to try and break the link between what you have learned from other languages and think of Python by itself.<BR/><BR/>So, in the first example, "once" is a synonym given to object "1". A further assignement like <BR/>une_fois = once<BR/>is equivalent to<BR/>une_fois = 1<BR/>where the "synonym" 'once' has been replaced on the right hand side by the object it "refers" to.<BR/><BR/>It does not matter whether the object on the right hand side is a scalar, a list or some more complicated object.<BR/><BR/>Note that '1' is an object that can not change (all numbers are like that).<BR/><BR/>In the second example, "groceries" and "epicerie" are two 'synonyms' that refer to the same object. Whether you manipulate this object (change its content, since it is not a scalar) through one or the other synonym does not alter the fact that there is only one object that is changed. That object (a list) is a mere container for other objects. <BR/><BR/>You could take the second example, and add at the end<BR/>groceries = 3.<BR/>Now, "groceries" has been given another meaning and only "epicerie" remains as a synonym to the list.<BR/><BR/>Did this help?<BR/><BR/>AndréAndré Robergehttps://www.blogger.com/profile/08131391818998844540noreply@blogger.comtag:blogger.com,1999:blog-9266717.post-1117125444448391352005-05-26T13:37:00.000-03:002005-05-26T13:37:00.000-03:00I'm not a Python programmer (actually not a progra...I'm not a Python programmer (actually not a programmer by profession, more of a systems person) so perhaps you can confirm what I'm getting from this article.<BR/><BR/>Scalar assignments (like:<BR/>une_fois = once<BR/>) are assignments by value ('une_fois' takes the value that 'once' has at the time of the assignment and remains unaffected by subsequent assignments to 'once'?<BR/><BR/>List assignments (like:<BR/>epicerie = groceries<BR/>) are assignments by reference ('epicerie' as a list changes value when 'groceries' as a list changes value)?<BR/><BR/>Or should I be thinking of list variables in Python as references to lists, thus the assignment:<BR/>epicerie = groceries<BR/>is really copying the value of groceries, which is a reference to a list, thus when the list is changed by accessing it through 'groceries' reference, the same list is seen to be changed when accessing it through the 'epicerie' reference?<BR/><BR/>Confusedly yours,<BR/>BruceAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9266717.post-1117035368816723282005-05-25T12:36:00.000-03:002005-05-25T12:36:00.000-03:00Regarding the confusion that I created, according ...Regarding the confusion that I created, according to the last comment (and taking it at face value):<BR/><BR/>The reality is that for *many* people introduced to computer programming, statements like N=N+1 are confusing. If you read about computer language design and pedagogy, you will find that the problem of using "=" as an assignment operator is often noted. This is why, as I mentioned in the article, some languages use a different symbol (like :=, or a left-arrow) to denote variable assignment.<BR/><BR/>I suspect that most people that become successful programmer are those who, like you, were not confused by this. I was never confused by it myself (from way back to the old Fortran days ;-) but have since read about this problem faced by many.André Robergehttps://www.blogger.com/profile/08131391818998844540noreply@blogger.comtag:blogger.com,1999:blog-9266717.post-1117029394628397072005-05-25T10:56:00.000-03:002005-05-25T10:56:00.000-03:00When I opened this article the thought in my mind ...When I opened this article the thought in my mind was "= operator hard, can't remember ever being confused by that"<BR/>But you succeded to create that confusion by your examples, impressive!<BR/><BR/>;)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9266717.post-1116937309629107552005-05-24T09:21:00.000-03:002005-05-24T09:21:00.000-03:00Excellent ideas and nicely written article. It's o...Excellent ideas and nicely written article. It's only to hope that our students are at ease with juggling 3-4 human languages aside a new programming one ;-) Not easily achieved, in my experience.Anonymousnoreply@blogger.com