Existing two files, one file name is ideal.py, the other is hub.py, both are in the directory Modualsio.
The specific contents of ideal are:
num = 3
Class Foo (object):
Global num
def foo (self):
Return num*3
PS: In this usage, if we do not use global to declare the global variable num in function foo, it is not actually available, but it should be used as an internal variable, because there is no initial value, so the error occurs. The Python lookup variables are in the order: first the local variable, then the global variable.
The specific contents of hub.py are:
Import Ideal
Foo=ideal. Foo () #调用ideal模块后, instantiate class Foo in hub.py.
Print Foo.foo () #然后用实例化的foo () method.
Print Ideal.name
Run the scripts and get the output as
3
9
In addition, it is worth summing up: the difference between the return and print statements in the function is that the meaning of print is the direct output meaning, return is only returned, to be able to output the return after the content must also use the Print statement (return can be followed by a string, expressions, and so on).
Python module invocation and global usage