Python module invocation and global usage

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.