Python Forum
Is This Code Ok? How Can I Avoid Using Global Variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is This Code Ok? How Can I Avoid Using Global Variables?
#8
(Feb-14-2018, 09:29 AM)DeaD_EyE Wrote:
def test():
 return [1, 2, 3, 4, 5]
 
 
def test2():
    return [6, 7, 8, 9, 10]
 

def test3():
    return [11, 12, 13, 14, 15]


final_list = test1() + test2() + test3()
print()
print(final_list)

I appreciate the help once again, DeaD_EyE Thumbs Up!

I think I understand 'return' well enough to use it in my projects now.

Part of my confusion was passing multiple objects into a single return, then combining them outside the function, but it seems easy in this case (multiple lists I want to combine). I'm still not 100% sure how to return different object types, like for example a string and a list through return, because then it's like they're combined into a single variable when you call the function.

Is the rule to only return 1 kind of data per function?

def test1():

    link_list1 = [1, 2, 3, 4, 5]
    link_list2 = [6, 7, 8, 9, 10]
    link_list3 = [11, 12, 13, 14, 15]

    return link_list1 + link_list2 + link_list3


def test2():

    link_list4 = [16, 17, 18, 19, 20]
    link_list5 = [21, 22, 23, 24, 25]
    link_list6 = [26, 27, 28, 29, 30]

    return link_list4 + link_list5 + link_list6


final_list = test1() + test2()
print()
print(final_list)
This forum is the best, its helped me overcome all the brick walls I've hit so far, you guys are amazing.
Reply


Messages In This Thread
RE: Is This Code Ok? How Can I Avoid Using Global Variables? - by digitalmatic7 - Feb-14-2018, 10:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,162 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,049 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,175 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 971 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Avoid third party functions to wrote my python code into system debug-log? mark 9 2,241 Apr-09-2022, 08:41 PM
Last Post: mark
  Global variables not working hobbyist 9 4,776 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,687 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,294 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Question regarding local and global variables donmerch 12 5,154 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,464 Apr-08-2020, 06:01 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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