码迷,mamicode.com
首页 > 编程语言 > 详细

python package 的两种组织方式

时间:2016-05-26 06:23:31      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

 

方式一
/package1/

.../__init__.py

        # 空文件

 

.../class1.py

        class Class1:
            def __init__(self):
                self.name = "class one"
            
            def printInfo(self):
                print("i am class One!")


.../class2.py

        class Class2:
            def __init__(self):
                self.name = "class two"
            
            def printInfo(self):
                print("i am class two!")


/demo1.py

    from package1.class1 import Class1
    from package1.class2 import Class2

    if __name__ == "__main__":
        c1 = Class1()
        c1.printInfo()
        c2 = Class2()
        c2.printInfo()

 



#####################################

方式二

/package2/
.../__init__.py

        from .class1 import Class1
        from .class2 import Class2

 

... /class1.py

        class Class1:
            def __init__(self):
                self.name = "class one"
            
            def printInfo(self):
                print("i am class One!")


.../class2.py

        class Class2:
            def __init__(self):
                self.name = "class two"
            
            def printInfo(self):
                print("i am class two!")

 

/demo2.py

    from package2 import Class1, Class2

    if __name__ == "__main__":
        c1 = Class1()
        c1.printInfo()
        c2 = Class2()
        c2.printInfo()

 




 

python package 的两种组织方式

标签:

原文地址:http://www.cnblogs.com/hhh5460/p/5529521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!