This is my project structure:
a├── b.py└── __init__.py
File __init__.py
is one line:
b = 'this is a str'
Then the following program gives inconsistent result of a.b
:
import a
print(a.b) # str
import a.b
print(a.b) # module
Last Updated : 28 Jul, 2022
Now the interpreter is given the command to run File1.py.
python File1.py
Output:
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output:
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly
What are the “best practices” for using import in a module?,Why are default values shared between objects?,How do I share global variables across modules?,You could use a global variable containing a dictionary instead of the default value; it’s a matter of taste.
>>> x = 10 >>> def bar(): ...print(x) >>> bar() 10
>>> x = 10 >>> def foo(): ...print(x) ...x += 1
>>> foo()
Traceback(most recent call last):
...
UnboundLocalError: local variable 'x'
referenced before assignment
>>> x = 10 >>>
def foobar():
...global x
...print(x)
...x += 1 >>>
foobar()
10
>>> print(x) 11
>>> def foo():
...x = 10
...def bar():
...nonlocal x
...print(x)
...x += 1
...bar()
...print(x) >>>
foo()
10
11
The above error occurs because, when an assignment is made to a variable in a scope, Python automatically considers that variable to be local to that scope and overlooks any similarly named variable in any outer scope.,This error is common because it is easy to confabulate assignments of functions to local and global variables in Python. However, this should be avoided because this can immensely hamper productivity, wasting time and effort because of the confusion that can also arise within development.,This is not recommended but a global variable can be created by declaring a function as global. Here is some code below that demonstrates this.,CAST recommends avoiding conflicts between local and global variables because of the lack of productivity that can occur in the code. Local variables are automatically created in Python unless declared as global.
bad
<xxx> = ..
...
def fct():
...
<xxx> = ...
good
<xxx>
...
def fct():
...
<yyy> = ...
>>> x = 10
>>> def foo():
... x += 1
... print x
...
>>> foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'x' referenced before assignment
This is not recommended but a global variable can be created by declaring a function as global. Here is some code below that demonstrates this.
globvar = 0 def set_globvar_to_one(): global globvar # Needed to modify global copy of globvar globvar = 1 def print_globvar(): print(globvar) # No need for global declaration to read value of globvar set_globvar_to_one() print_globvar() # Prints 1
Variable name errors come with some of the most informative error messages, which are usually of the form “name ‘the_variable_name’ is not defined”.,Run the code, and read the error message. What type of NameError do you think this is? In other words, is it a string with no quotes, a misspelled variable, or a variable that should have been defined but was not?,Another very common type of error is called a NameError, and occurs when you try to use a variable that does not exist. For example:,A NameError will occur if you use a variable that has not been defined, either because you meant to use quotes around a string, you forgot to define the variable, or you just made a typo.
# This code has an intentional error.You can type it directly or
# use it
for reference to understand the error message below.
def favorite_ice_cream():
ice_creams = [
"chocolate",
"vanilla",
"strawberry"
]
print(ice_creams[3])
favorite_ice_cream()
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-1-70bd89baa4df> in <module>()
6 print(ice_creams[3])
7
----> 8 favorite_ice_cream()
<ipython-input-1-70bd89baa4df> in favorite_ice_cream()
4 "vanilla", "strawberry"
5 ]
----> 6 print(ice_creams[3])
7
8 favorite_ice_cream()
IndexError: list index out of range
def some_function()
msg = "hello, world!"
print(msg)
return msg
File "<ipython-input-3-6bb841ea1423>", line 1
def some_function()
^
SyntaxError: invalid syntax
def some_function():
msg = "hello, world!"
print(msg)
return msg
File "<ipython-input-4-ae290e7659cb>", line 4
return msg
^
IndentationError: unexpected indent