Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
05-14-2024, 12:47 AM
|
#1
|
Member
Registered: Jun 2023
Posts: 380
Rep:
|
Why append to list is not the same as +=
The first method uses list comprehension, and appends the string in the array 'names' satisfying the given criteria.
First code>
Code:
names=['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
best_list = [name for name in names if len(name)>=6]
print(best_list)
Output:
Code:
['Kramer', 'Elaine', 'George', 'Newman']
----------------------------------------------------------------------
The second method also does the same, as above.
Second code>
Code:
names=['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
better_list=[]
for name in names:
if len(name)>5:
better_list.append(name)
print(better_list)
Output:
Code:
['Kramer', 'Elaine', 'George', 'Newman']
----------------------------------------------------------------------------------
If change the second code to the one below, then the individual characters, are stored:
Third code>
Code:
names=['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
better_list=[]
for name in names:
if len(name)>5:
better_list+=name
print(better_list)
Output:
Code:
['K', 'r', 'a', 'm', 'e', 'r', 'E', 'l', 'a', 'i', 'n', 'e', 'G', 'e', 'o', 'r', 'g', 'e', 'N', 'e', 'w', 'm', 'a', 'n']
-----------------------------------------------------------------------------------------------------
Question: a) Why the third code, saves the individual characters, is unclear.
b) Also, how can the first code be modified to give the same output as the third code?
|
|
|
05-14-2024, 02:52 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,733
|
yes, that looks strange, but actually a string is an array of chars and += will just concatenate the two arrays (left side + right side).
append will add a single item to the list/array, which is the string itself.
|
|
|
05-14-2024, 07:24 AM
|
#3
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,863
|
And this sort of thing is definitely(!) where "D'oh!" problems come about. And why this web-site has both this built-in emoji and this one:
In many languages, not just Python, there are subtly-different things which, "at first glance," appear to be the same. But they're not. So, when you are "reading the source code, looking for that damned bug," you might fail to see it for a long time. Because of course you "see, at a glance" what you expect to see. Even if the language designer thought that some different behavior was "a fee-chur."
In Python, the "+=" operator was so-called overloaded in this way. But it is not the same as .append(). Even though it looks like it is, or ought to be.
And, worse yet – another language-designer might have made an entirely different choice. (In fact, "I once wrote a programming language of my own" , and in that project I did.) So, "your experience with other languages" can actually lead to an unexpected and extremely-annoying surprise.
P.S.: This is one reason why I've introduced all of my programming teams to "the five-minute rule." If you don't see what's wrong with a piece of code after five minutes – and, having tried as best you can to zero-in on where "the bug" must be – ask for another pair of eyes. I explicitly tell the team that "anyone who does this is not 'interrupting you.'"
Last edited by sundialsvcs; 05-14-2024 at 07:32 AM.
|
|
|
05-14-2024, 11:31 PM
|
#4
|
Member
Registered: Jun 2023
Posts: 380
Original Poster
Rep:
|
Quote:
Originally Posted by sundialsvcs
And this sort of thing is definitely(!) where "D'oh!" problems come about. And why this web-site has both this built-in emoji and this one:
In many languages, not just Python, there are subtly-different things which, "at first glance," appear to be the same. But they're not. So, when you are "reading the source code, looking for that damned bug," you might fail to see it for a long time. Because of course you "see, at a glance" what you expect to see. Even if the language designer thought that some different behavior was "a fee-chur."
In Python, the "+=" operator was so-called overloaded in this way. But it is not the same as .append(). Even though it looks like it is, or ought to be.
And, worse yet – another language-designer might have made an entirely different choice. (In fact, "I once wrote a programming language of my own" , and in that project I did.) So, "your experience with other languages" can actually lead to an unexpected and extremely-annoying surprise.
P.S.: This is one reason why I've introduced all of my programming teams to "the five-minute rule." If you don't see what's wrong with a piece of code after five minutes – and, having tried as best you can to zero-in on where "the bug" must be – ask for another pair of eyes. I explicitly tell the team that "anyone who does this is not 'interrupting you.'"
|
Request if there was a pointer to the source code of overloaded '+' operator, in order to see how the bug appeared.
|
|
|
05-15-2024, 01:01 AM
|
#5
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,733
|
Quote:
Originally Posted by ajiten
Request if there was a pointer to the source code of overloaded '+' operator, in order to see how the bug appeared.
|
it is not a bug, it is how it is implemented, designed. It is intentional. You need to check the python source code yourself if you want to go into details. Do not request again python internals.
|
|
1 members found this post helpful.
|
05-15-2024, 04:29 PM
|
#6
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,863
|
@pan64: All I am saying is that “different popular programming language [designers …] might do things very differently. So, a source-code statement that you have written before … in a different world … might now behave differently here. But, “no syntax errors!”
I particularly encountered this issue because I took on “existing [legacy?] software,” in whatever language it might be [have been]. (It isn’t pretty …) Many languages do things like “overloading operators,” but they do it in an interpreter context and the meaning is not always what you(!) “intuitively” expect it to be.
Last edited by sundialsvcs; 05-15-2024 at 04:30 PM.
|
|
|
05-15-2024, 05:00 PM
|
#7
|
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,335
|
Quote:
Originally Posted by ajiten
Request if there was a pointer to the source code of overloaded '+' operator, in order to see how the bug appeared.
|
You don’t need the source code. You have documentation.
https://python-reference.readthedocs...est/docs/list/
|
|
|
05-15-2024, 06:21 PM
|
#8
|
Member
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 547
|
Yep. Not a bug. Just the way it works. Reference the List document above for working with 'lists'.
|
|
|
05-16-2024, 02:25 AM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,733
|
Quote:
Originally Posted by sundialsvcs
@pan64: All I am saying is that “different popular programming language [designers …] might do things very differently. So, a source-code statement that you have written before … in a different world … might now behave differently here. But, “no syntax errors!”
I particularly encountered this issue because I took on “existing [legacy?] software,” in whatever language it might be [have been]. (It isn’t pretty …) Many languages do things like “overloading operators,” but they do it in an interpreter context and the meaning is not always what you(!) “intuitively” expect it to be.
|
Yes, I know that. It is called: do not assume anything (but read the documentation).
|
|
|
05-16-2024, 06:16 AM
|
#10
|
Senior Member
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,020
|
Operator overloading is a bad feature, IMO. The available operators are few in number compared to methods. When reading code that uses overloading, one can never be sure what "a+b" does.
Ed
|
|
2 members found this post helpful.
|
05-16-2024, 07:15 AM
|
#11
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,733
|
Quote:
Originally Posted by EdGr
Operator overloading is a bad feature, IMO. The available operators are few in number compared to methods. When reading code that uses overloading, one can never be sure what "a+b" does.
Ed
|
Not really. As any other thing in this world it can be used properly and can be abused. The other wisdom is: (do not assume anything, but) learn the tool you use otherwise it won't work.
|
|
|
05-16-2024, 07:22 AM
|
#12
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,928
|
Off: which one is the right usage of the += operator:
Code:
birthday += 3; /* 3 days, probably */
rocketLaunchTime += 3; /* 3 hours or 3 sec? */
Last edited by NevemTeve; 05-16-2024 at 07:24 AM.
|
|
|
05-16-2024, 10:32 AM
|
#13
|
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,335
|
The only thing I see wrong with the above code sample is that birthday should probably be const.
|
|
|
05-29-2024, 04:59 PM
|
#14
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,863
|
I heartily(!) agree with @EdGr in his assessment: "operator overloading" is an idea which has led to much grief.
In the easy case, it was "the language designer" who committed the foul deed. Whereas, in the case that I actually encountered, it was the "too clever by half" application designer(!) who did it.
The most(!) fundamental requirement of "any application source-code" is that anyone who is not 'thoroughly familiar with it' must be able to understand "at a glance" what he is seeing. Otherwise, (s)he might well be looking at "the bug" and not know it.
Well, I once encountered "the bug," which turned out to be in third-party software, which consisted of two lines of source-code which needed to be reversed. And, this bug cost me $10,000. Real dollars. I had made a contractual commitment, and I honored it, even though I was not the one "at fault." (My relationship with the client continued in good faith, who understood, and I was able to recover most of the money.)
|
|
|
05-30-2024, 12:51 AM
|
#15
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,733
|
Quote:
Originally Posted by sundialsvcs
I heartily(!) agree with @EdGr in his assessment: "operator overloading" is an idea which has led to much grief.
|
No,I don't agree . Operator overloading is a very nice and useful feature, if it is used properly. You are right, it is often misused and leads to grief, but not because of its nature, but because of the nature of the developer who has no idea what they are doing.
I really hate the case when someone blames the tool when they can't use it.
|
|
|
All times are GMT -5. The time now is 08:39 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|