假设自己写的 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)

相关文章:

  • 2021-10-24
  • 2022-12-23
  • 2021-03-27
  • 2022-12-23
  • 2021-05-16
  • 2022-12-23
  • 2021-08-03
  • 2021-11-02
猜你喜欢
  • 2022-12-23
  • 2022-02-28
  • 2021-09-10
  • 2022-12-23
  • 2022-12-23
  • 2021-07-05
  • 2021-04-09
相关资源
相似解决方案