0

Two .py files, the to_be_imported.py has:

def func(a):

    b = a + "!!!"
    c = b + " Mike!!!"
    print c

The import.py has:

from to_be_imported import *
func("hey")

But when I try to access variable b I got error AttributeError: 'NoneType' object has no attribute 'b'..

How to get the value of b after I give the function value "hey"?

5
  • please post the error. We cant do anything with I got error Commented Apr 27, 2017 at 18:35
  • You should read about local and global variables in Python docs.python.org/3/faq/… Commented Apr 27, 2017 at 18:37
  • 2
    Your posted code has no reference to a remote symbol b; the code does not show the error you describe. Commented Apr 27, 2017 at 18:42
  • func returns None, since you don't have an explicite return statement. This, when you assign var = func('hey'), then var == None and when you try var.b you'll get the error you are seeing. Furthermore, even if var was not None, you can't access the local variables in func that way, unless you do some hocus-pocus with locals() and return that... but that sounds like a fundamental design issue with your code. In general, if you want some value inside some function, your function should return that value. Commented Apr 27, 2017 at 18:44
  • You can only access top-level module variables (aka module globals) from another script/module. You can never access the local variables of a function outside the function unless they are the or one of the return values of the function. See this answer to a question about Python's scoping rules. Commented Apr 27, 2017 at 18:59

2 Answers 2

1

to_be_imported does not have a variable b. The only b in your program is local to func, and disappears when you exit the function. The canonical way to do this would be:

def func(a):

    b = a + "!!!"
    c = b + " Mike!!!"
    print c
    return b

...

from to_be_imported import *
local_b = func("hey")

There are other ways to do this. For instance, you could be make b a global variable of to_be_imported and then access it with something like

print to_be_imported.b

However, this is generally not a good idea. Also, note that it's not really a good idea to have a remote function both print output and return a value. Modules and passing information are really cool, but make sure you follow recommendations in the textbook or tutorials you're using, so you don't have debugging troubles later.

Sign up to request clarification or add additional context in comments.

Comments

0

You might consider returning the value, like in this example:

def add_one(numb):
    """Given a number, add one and return it."""
    r = numb +1
    return r

Also note that it's generally bad practice to do from module import *, as it can overwrite functions in the module that does that import. You might try importing just what you need, like: from mymodule import func, func_two, func_three

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.