Import external class files using Python

Source: Internet
Author: User

GenerallyCodeThe code used to call a class to create an object is rarely stored in the same file. This problem occurs when one file needs to call another independently stored class file to create an object. There are few introductory books and online textbooks mentioned in this regard. The following example shows the implementation method:

T1.py:

# Here is the file defining the class CAL: def sum (self, a, B): return a + B def Cha (self, a, B): return a-B

T2.py:

# Here is the file for creating the object from T1 import Cal # The syntax format of the import class is: From file name import class name c = CAL () B = C. cha (1, 2) print (B)

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.