0

Contents of file_one.py:

from file_two import *

def function_one():
    my_list = []
    function_two()

function_one()

Contents of file_two.py:

def function_two():
    my_list.append(1)
    print my_list

Error on running 'file_one.py':

NameError: global name 'my_list' is not defined

The above example is a simplified version of what i am actually trying to implement however it produces the same error and i believe it replicates the same logic.

In the actual implementation, as a newbie, i was importing a file full of functions (file_two.py) into file_one.py and then just using the function name in file_one.py when i wanted to call it.

This worked fine until i had to use a variable from a function in file_one.py in a function in file_two.py (as demonstrated above). Hence my need to understand classes.

This seems to be a very succinct description and implementation of classes:

https://stackoverflow.com/a/10139935/1063287

However i don't (that i can currently see) need to use a function from a class, just a variable.

I get stuck here thinking whether i just need to create a simpler class file (without functions) and if so:

  • what would be required to be in it (in regards to syntax etc)?
  • and how would i reference the variables in it from elsewhere (again in regards to syntax etc)?
Community
  • 1
  • 1
user1063287
  • 10,265
  • 25
  • 122
  • 218
  • 3
    I don't quite understand what you're trying to achieve here... why can you not pass `my_list` as a function parameter to `function_two()`? – Geekfish Apr 30 '13 at 10:05
  • thank you, i'm not sure how scalable this will be in the future (so i will have to learn more about classes), but i had not thought of that and passing the variables through as parameters worked a treat. thank you. – user1063287 May 01 '13 at 04:14

2 Answers2

0

I don't think usage of classes is essential to achieve some sort of state:

state.py:

my_list = []

file_two.py:

import state

def function_two():
    state.my_list.append(2)
    print state.my_list

file_one.py:

from file_two import function_two
import state

def function_one():
    state.my_list.append(1)
    function_two()

function_one()
# this will print out [1, 2]

Of course you could use classes as well, but it doesn't sound like you really need to.

I would recommend against using global although it could work, I find it makes less clear where variables are coming from.

UPDATE:

Of course if all you want to do is make a variable from function_one available in function_two you can always just pass it as a parameter like this:

file_two.py

def function_two(some_list):
    some_list.append(2)
    print some_list

file_one.py

from file_two import function_two

def function_one():
    my_list = []
    my_list.append(1)
    function_two(my_list)

function_one()
# [1, 2]

Objects and classes are quite powerful, but they're not necessary to write well-structured/scalable code (that's to address the concern you expressed in your comment above). Passing parameters is a basic component of programming in Python, you should find yourself using it all the time wether you're making a very basic or a very complex application.

Geekfish
  • 2,173
  • 23
  • 28
  • i used your original suggestion of passing the variables through as function parameters. if you'd like to add that suggestion to this reply then i can mark it as the correct answer. thank you again. – user1063287 May 01 '13 at 04:16
0

As pointed out, the use of classes is not essential, then the code below is really poor but gives you an idea of 'How to use classes to access a variable '

def function_two():
    function_one.my_list.append(1)
    print function_one.my_list


class function_one():
    my_list = [] 
    @staticmethod
    def __call__():
        function_one.my_list = []
        function_two()
function_one = function_one()

Now function one behaves as a function and has an attribute my_list accessible from outside.

But you should consider passing my_list to finction_two as a parameter...

jimifiki
  • 5,377
  • 2
  • 34
  • 60