您的位置:首页 > 编程语言 > Python开发

Python: import Module and Class

2013-11-29 21:09 549 查看
Module: function_set.py

def print_function( name ):
print "hello, %s" % name;
return


Class: Person.py

class Person:
'''
classdocs
'''
name = "";
age = 0;
gender = "Unknown";

def __init__(self, name, age, gender):
'''
Constructor
'''
self.name = name;
self.age = age;
self.gender = gender;

def print_info(self):

print "Name: %s, Age: %d, Gender: %s" % (self.name, self.age, self.gender);


import:

from function_set import print_function
from Person import Person

print_function("tes1t");

p = Person("John Smith", 28, "Male");

p.print_info();


Note:

Each package in Python is a directory which MUST contain a special file called __init__.py.
This file can be empty, and it indicates that the directory it contains is a Python package,
so it can be imported the same way a module can be imported.

http://www.learnpython.org/Modules_and_Packages
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: