Python 调用自己编写的Class

假设自己写的 class 文件myPets.py放在当前目录的子目录/myClasses下,在myPets.py中定义了一个 classPet。现在要调用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)
原文地址:https://www.cnblogs.com/yaos/p/6992282.html