Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Is This Code Ok? How Can I Avoid Using Global Variables?
#1
I've heard from multiple sources I should try to avoid using global variables, since it's probably a sign of broken / badly written code. Here's an example of what I need to do:

list1 = None
list2 = None
list3 = None


def test():

    global list1
    list1 = [1, 2, 3, 4, 5]


def test2():

    global list2
    list2 = [6, 7, 8, 9, 10]


def test3():

    global list3
    list3 = [11, 12, 13, 14, 15]


test()
test2()
test3()

final_list = list1 + list2 + list3

print()
print(final_list)


'''

    in the full script LOTS of actions will be taken inside each function to generate the lists,
    this code is just for a very basic example of what I plan to do.

'''
I'm aware of 'return' and 'function parameters', I've been researching them in my free time today, but I can't get them to replace global variables in this situation.
Quote
#2
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)
digitalmatic7 likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#3
to elaborate further, based on DeaD_EyE's exmaple:
you can also assign what a function has returned to a variable to preserve and use latter in the code
my_list = test()
digitalmatic7 likes this post
Quote
#4
if we replace the outside list from None to []... then append it inside function, should work the same. no parameters,return or global keywords needed. list id also preserved. jz repeat the same for test2() and test3(). did this in python 2.7 just in case error occured

list = []

def test():
 for a in range(1,6):
    list.append(a)
test()
Output:
list = [1,2,3,4,5]
swallow osama bin laden
Quote
#5
OP wants to avoid using global variable (good).
Your code is poor because it uses global variable, change it using mutability of list and uses built-in function list() as variable name. Don't do this...
Quote
#6
thx, will keep in mind on that variabe name

if preserving id is the reason im using global variable...
OP uses global variable , yet the list id changed , same goes to my_list = test()
so either way we end up using global variable?
swallow osama bin laden
Quote
#7
the idea is to keep namespaces separated and the scope of variables as narrow as possible. in the OP original code and your code you mix the global module namespace and the local function namespace
digitalmatic7 likes this post
Quote
#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.
Quote
#9
you can return anything (i.e. any object), incl. some complex data structure like list/tuple of mixed element types, or dict, etc.
Quote
#10
You can return everything:

def foo():
    return {}, 'Foo', 42, 1337.13, [], ()
Implicit all arguments are packed into a tuple.
Afterwards you can unpack the objects.


Dict, String, Integer, Float, List, Tuple = foo()
Now the names refer to the objects.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 210 Feb-09-2020, 02:05 PM
Last Post: new_to_python
  Please help me understand how to use global variables joew 6 272 Jan-05-2020, 06:03 PM
Last Post: joew
  Global Variables - Some Points Needing Clarification. adt 4 284 Nov-30-2019, 01:23 PM
Last Post: adt
  modifying variables in local or global space Skaperen 2 232 Aug-14-2019, 07:13 AM
Last Post: Skaperen
  Global variable does not seem to be global. Columbo 6 494 Jul-15-2019, 11:00 PM
Last Post: Columbo
  passing an argument to avoid a global Skaperen 9 536 Jul-12-2019, 11:07 PM
Last Post: Skaperen
  Global variables not changing ThemePark 3 489 Mar-02-2019, 11:24 AM
Last Post: Larz60+
  help with threading module and passing global variables ricardons 1 1,259 Feb-21-2019, 12:48 PM
Last Post: stullis
  Use of global variables from several modules. Jstechg 3 576 Jan-03-2019, 03:39 AM
Last Post: scidam
  Resetting Global Variables? WuchaDoin 11 1,335 Sep-28-2018, 08:41 PM
Last Post: WuchaDoin

Forum Jump:


Users browsing this thread: 1 Guest(s)