假设自己写的 class 文件myPets.py
放在当前目录的子目录/myClasses
下,在myPets.py
中定义了一个 class 叫Pet
。现在要调用Pet
这个 class :
from myClasses.myPets import Pet
ginger = Pet("Ginger", "Cat")
myPets.py
:
class Pet(object):
'''
object is a special variable in Python that you should include
in the parentheses when you are creating a new class.
'''
def __init__(self, name, species):
self.name = name
self.species = species
def getName(self):
return self.name
def getSpecies(self):
return self.species
def __str__(self):
return "%s is a %s" % (self.name, self.species)