http://blog.csdn.net/liukang325/article/details/46724365

建立一个文件夹filePackage 
在filePackage 文件夹内创建 __init__.py 
有了 __init__.py ,filePackage才算是一个包,否则只是算一个普通文件夹。

在filePackage 文件夹内创建 file.py 
file.py 代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*- from datetime import datetime class MyFile(): def __init__(self, filepath):
print('MyFile init...')
self.filepath = filepath def printFilePath(self):
print(self.filepath) def testReadFile(self):
with open(self.filepath, 'r') as f:
s = f.read()
print('open for read...')
print(s) def testWriteFile(self):
with open('test.txt', 'w') as f:
f.write('今天是 ')
f.write(datetime.now().strftime('%Y-%m-%d'))

__init__.py 代码如下:

from file import MyFile

把本模块里面的 公用的类 方法 暴漏出来

然后 外面的引用 不用找到具体的现实位置,找到包的__init__ 就好了


建立main.py 和 filePackage 平级, 
main.py 代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*- from filePackage import MyFile if __name__ == '__main__':
a = MyFile("./filePackage/test.txt")
a.printFilePath();
a.testReadFile();

目录结构: 


若 __init__.py 里什么也不写,那么在main.py里也可以这样写:

import filePackage.file
if __name__ == '__main__':
a = filePackage.file.MyFile("./filePackage/test.txt")
a.printFilePath();

但不建议这样写,建议按上面的方法将模块里的公用类暴露出来,直接引用。

[转] Python包和类的基本用法的更多相关文章

  1. Python 基础教程之包和类的用法

    Python 基础教程之包和类的用法 建立一个文件夹filePackage 在filePackage 文件夹内创建 __init__.py 有了 __init__.py ,filePackage才算是 ...

  2. RobotFramework中加载自定义python包中的library(一个py文件中有多个类)

    结构如下: appsdk\ appsdk.py(这里面有多个类,包括appsdk,appsdksync等类) __init__.py ... ① 有个appsdk的文件夹(符合python包的定义) ...

  3. [python基础]关于包,类,模块的那些事儿

    转载请注明出处:http://www.cnblogs.com/codefish/p/5032753.html 在理解python的包,类,模块之前,我一直是将他类比为dll,C#的类,命名空间的这种参 ...

  4. Python + OpenCV2 系列:3 - python 字符串,类,编码规范

    首先,强烈推荐<<简明 Python 教程>> Swaroop, C. H. 著 沈洁元 译 其实,这本书里已经把python的最基本的用法,编码等等介绍的很好,这里把我用到的 ...

  5. 【Python使用】使用pip安装卸载Python包(含离线安装Python包)未完成???

    pip 是 Python 包管理工具,该工具提供了对Python包的查找.下载.安装.卸载的功能.Python 2.7.9 + 或 Python 3.4+ 以上版本都自带 pip 工具. pip使用( ...

  6. python 包和模块

    一. 模块 使用内置函数vars()可以查看当前环境下有哪些对象(变量.函数.类) from 模块 import *: 不会导入以下划线开头的对象 只会导入__all__中定义了的对象(__all__ ...

  7. 全面学习 Python 包:包的构建与分发

    首发于公众号:Python编程时光 1. 为什么需要对项目分发打包? 平常我们习惯了使用 pip 来安装一些第三方模块,这个安装过程之所以简单,是因为模块开发者为我们默默地为我们做了所有繁杂的工作,而 ...

  8. python 包(package)和模块(module)的创建和引入(import)

    python 包(package)和模块(module)的创建和引入(import) 名词解释 实际上,Python中的函数(Function).类(Class).模块(Module).包库(Pack ...

  9. 简单说明Python中的装饰器的用法

    简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下   装饰器对与 ...

随机推荐

  1. Scramble String

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  2. RouteArea中AreaPrefix(Area 前缀)的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  4. AndroidStudio 中的坑Error:(1, 0) Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRID

    将 build.gradle 中 的 classpath改为2.0.+ dependencies { classpath 'com.android.tools.build:gradle:2.0.+'然 ...

  5. Win7打补丁以后vs2012突然出现的程序版本不兼容问题

    装这个: Update for Microsoft Visual Studio 2012 (KB2781514) http://www.microsoft.com/en-us/download/det ...

  6. neutron创建network执行的那些命令

    当搭建完openstack之后,在创建instance之前,第一件事情就是创建network,一个经典的流程如下: TENANT_NAME="openstack"TENANT_NE ...

  7. SPRING + THYMELEAF 配置

    1.使用的是Spring EL而不是Ognl.2.访问上下文的Bean用${@myBean.doSomething()}3.th:field,th:errors,th:errorclass用于form ...

  8. require.js+backbone.js基本使用

    一.菜单{ //银行卡基础信息维护 rsId: 'menu.param.cardbin', iconCls: 'icon-double-angle-right', name: menusLang._( ...

  9. 关于CGContextSetBlendMode: invalid context 0x0的错误

    在ios 7的模拟器中,选择一个输入框准备输入时,会触发这个错误,以下是出错详细日志: <Error>: CGContextSetBlendMode: invalid context 0x ...

  10. Requests库上传文件时UnicodeDecodeError: 'ascii' codec can't decode byte错误解析

    在使用Request上传文件的时候碰到如下错误提示: 2013-12-20 20:51:09,235 __main__ ERROR 'ascii' codec can't decode byte 0x ...