Python 的长代码文件怎么组织

c/c++ 能够经过多页的头文件来组织一个较大的工程,而且容易维护,阅读和修改。那么python中是怎么实现的呢?python

与c中“include”关键字功能类似的是“import”。让咱们来对比一下用两种不一样语言实现相同功能的两段代码:c++

================C言语版==============================get

//--------------------Welcome.h-----------------------------------------------it

#ifndef WELCOME_Hast

#def WELCOME_Hclass

void hello(void);test

void bye(void);import

#endif引用

//--------------------Welcome.c------------------------------------------------im

include Welcome.h

void hello(void){

printf("Hello!");

}

void bye(void){

printf('Goodbye!');

}

//-------------------test.c------------------------------------------------------

include Welcome.h

int main(void){

hello();

bye();

}

===============python 版本=============================

--------------------------Welcome.py-----------------------------------------------

def hello():

    print "Hello!"

def bye():

    print "Goodbye!"

------------------------test.py------------------------------------------------------

import Welcome

Welcome.hello()

Welcome.bye()

本没打算做个对比的,只是想类比一下。仔细一看,是否是以为python言语更加简练,容易阅读呢。

若是定义了类,该怎么引用呢?用C++重写以上两段代码以下:


================C++言语版==============================

//--------------------Welcome.h-----------------------------------------------


#ifndef WELCOME_H

#def WELCOME_H


class Greeting{

public:

    Greeting();

    void hello(void);

    void bye(void);

}

#endif WELCOME_H

//--------------------Welcome.cpp------------------------------------------------

include Welcome.h

void Greeting:hello(void){

printf("Hello!");

}

void Greeting:bye(void){

printf('Goodbye!');

}

//-------------------test.c------------------------------------------------------

include Welcome.h

int main(void){

attendant.Greeting();

attendant.hello();

attendant.bye();

}

===============python 版本=============================

--------------------------Welcome.py-----------------------------------------------

class Greeting:

    def hello(self):

        print "Hello!"

    def bye(self):

        print "Goodbye!"

------------------------test.py------------------------------------------------------

import Welcome

attendant.Welcome.Greeting()

attendant.hello()

attendant.bye()


参考:

Python如何import自定义单一模块

相关文章
相关标签/搜索