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"?

martineau
  • 119,623
  • 25
  • 170
  • 301
DanZimmerman
  • 1,626
  • 6
  • 23
  • 45
  • please post the error. We cant do anything with `I got error ` – Mike - SMT Apr 27 '17 at 18:35
  • You should read about local and global variables in Python https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python – Seb Apr 27 '17 at 18:37
  • 2
    Your posted code has no reference to a remote symbol **b**; the code does not show the error you describe. – Prune Apr 27 '17 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*. – juanpa.arrivillaga Apr 27 '17 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](http://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules/292502#292502) to a question about Python's scoping rules. – martineau Apr 27 '17 at 18:59

2 Answers2

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.

Prune
  • 76,765
  • 14
  • 60
  • 81
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

oliver2213
  • 141
  • 4