Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which is more Pythonic?
#1
which is more Pythonic?

...
    foo = [some,stuff,goes,here]
    bar = a_function(arg1,foo,arg3)
...
or:

...
   bar = a_function(arg1,[some,stuff,goes,here],arg3)
...
i, personally, find the first way easier to read. does that count?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
The first one is better, because lesser code in function signature.
Additionally you have the reference to the list.

This can help you, if you have more than one function, which needs the list.
Then you can reuse the list in other function calls, but if you change the
list, everything is affected. This is why it's a bad idea to mutate a list
in a function.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
i would say the first one slightly more readable as well
Recommended Tutorials:
Reply
#4
what about single values like returned from a funtion, that may, or may not, be a list?

...
    foo = a_function(whatever)
    bar = b_function(arg1,foo,arg3)
...
vs.

...
    bar = b_function(arg1,a_function(whatever),arg3)
...
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Its almost always easier to read when you split it up like in the first example.
Recommended Tutorials:
Reply
#6
does almost count? (in this case)

i generally find it easier to read when things are split up like, including arithmetic where an intermediate value is a commonly named type of value. the variable name then documents what the code steps are coming up with.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  which is "better" (or more Pythonic)? Skaperen 2 2,017 Feb-01-2020, 03:10 PM
Last Post: Skaperen
  which is "better" (or more Pythonic)? Skaperen 7 3,139 Feb-01-2020, 03:51 AM
Last Post: Skaperen
  which is "better" (or more Pythonic)? Skaperen 8 3,217 Nov-16-2019, 06:46 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020