在python中的类之间传递属性

时间:2013-09-10 05:34:04

标签: python

我仍然不知道自己和初学者做了什么,尽管已经用尽了几个小时的阅读。我终于放弃了试图理解它的作用,而只是知道我需要将它包含在我的代码中(实际上我甚至不确定是否需要它)。

无论如何,为了学习这些东西,我已经在一些教程中找到了一个示例程序,并决定尝试自己复制它而不引用任何示例。

我可能这样做完全错了,但我要做的是有一个按下按钮创建新患者记录时调用的功能。我坚持的部分是如何将这些变量(它们被称为属性或变量?)从按钮按下功能传递给患者类?我是否只将该函数作为该类中的方法?另外,无论如何我每次按下按钮都可以创建新患者吗?

  def buttonpress():
      name = input("what's your name")
      age = input("what's your age")
      totalpatients +=1


  class patients:
      def __init__(self, name, age):
      self.name = name
      self.age = age

def displaypatient(self):
      print self.age, self.name

firstpatient=patients(name, 16)
firstpatient.displaypatient()

3 个答案:

答案 0 :(得分:2)

我觉得我几周前和你在同一条船上,尽管他们仍然非常痴心,但我会尽力向你解释自我并向你倾诉。请温柔,其他评论员! ;)

我将首先尝试解释“自我”,因为它可能会使init更清晰。当我想上课时,我可以通过以下方式完成:

class Car:
    # I can set attributes here ( maybe I want all my cars to have a:
    size = 0
    # well eventually my car object is going to need to do stuff, so I need methods - 
    # methods are like functions but they are only usable by the class that encompasses them!
    # I create them the same way, just inside the indentation of the class, using def func():
    def drive(self):
        print "vroom"
    # pretend this is the function that I would call to make the car drive.

好。我们在这里只讨论这个布局。我们已经确定了汽车应该做什么,但我们还没有做任何事情。 (我保证这一切都是相关的!) 为了进行汽车的INSTANCE(单次出现),我们可以将汽车对象分配给一个新的变量:

myCar = car()

现在我可以使用我们在汽车类中定义的所有方法 - 就像驱动器一样! 我们可以通过键入:

来调用该函数
myCar.drive()

这将打印“vroom” 我可以通过以下方式在同一个程序中创建第二个car()类的实例(尽管这将是一个完全不同的对象):

newCar = car()

现在,这里有一个初始部分...我已经完成了一个非常简单的课程,课程变得非常庞大和可怕,并且在一夜之间完全无法理解它们,但我要解释自己你现在。

“myCar”,我用来保存我所制造的汽车对象的变量,如果我有其他方法需要引用回那个对象,它就变成了“自我”参数。 本质上,myCar.vroom()与self.vroom()相同,如果我需要在该类的另一种方法中引用.vroom()。

将它包起来我们有类似的东西:

class Car:
    size = 0 # a global attribute for all car objects
    def drive(self): #self is the argument!
        print "vroom!"
myCar = car() # we've initialized the class but havent used it here yet!
myCar.drive() # this prints "vroom"

另一种想到这一点的方法就是说这个参数,就像在普通函数中一样,self就是一个占位符,无论哪个对象都在调用函数。

现在,如果这很有意义,如果没有,我会再次编辑它。 def init (self):使用相同的理论,从类中获取一个对象,并在每次类创建对象时提供指令。

class car:
    def __init__(self): # needs self, to refer to itself, right?
        self.name = name # now we can assign unique variables to each INSTANCE of the car class.

你可以使用def init 并使用它做一些疯狂的事情,就像在里面你可以立即调用其他方法和东西。基本上,它就像是说'嘿对象!你还活着,去查看 init 功能里面的内容,你需要拥有所有这些东西! GO!

让我知道这是否有帮助,或者我是否可以做得更清楚。就像我说的那样,我几乎只是围绕着这一切,所以也许我的解释需要一些工作。干杯:)

答案 1 :(得分:1)

您正在尝试使用基本的面向对象编程,但需要了解类定义对象并具有属性。

在下面的代码中,我们定义了一个Patient类:

class Patient:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display(self):
        return "%s - %s"%(self.age, self.name)

这有一个名为__init__的方法,它由Python定义为从类初始化新对象时调用的方法,并且有几个参数:

  • self - 是对我们正在创建的对象的引用,我们不会进入此处。但是现在明白传递给对象方法的第一个参数是对它自己的引用。
  • name - 我们定义的患者姓名
  • 年龄 - 我们也定义的年龄

第二种方法是我们可以调用对象的方法,让它返回一种我们可能想要打印出来的方法。

下面我们有创建患者的代码,并从显示方法中打印出它的表示。

firstpatient=patients(name, 16)
print firstpatient.display()

以下是一段代码,可用于构建一系列患者,供我们在以后阶段使用。

patients = [] # make an array with nopatients.
def getAnotherPatient():
    name = input("what's your name")
    age = input("what's your age")
    patients.append(Patient(name,age)) # Add a new patient
while somecondition:
    getAnotherPatient()

一旦我们在patients数组中构建患者列表,我们就可以遍历其中的对象,然后按照正常情况操纵或显示它们。

答案 2 :(得分:0)

基本上由数据(属性)和操作该数据的函数(方法)集合组成。类的每个实例都有自己的数据副本,但共享方法。

因此,类的每个 * 方法都在类的某个实例上运行,因此该方法需要知道它正在运行的实例。这就是self的用途 - self只表示方法正在使用的实例。

所以,例如:

class House:
    def renovate(self):
        ''' Renovate a house, increasing its value. '''
        self.value += 10000

oldhouse = House() # make a House instance that has no attributes
oldhouse.value = 100000 # set an attribute of the House
mansion = House()
mansion.value = 10000000
oldhouse.renovate() # self will be set to oldhouse, so that oldhouse's value goes up
print oldhouse.value # prints 110000
print mansion.value # prints 10000000: it hasn't changed because renovate knew what it was operating on

现在,在这个例子中,请注意我在课堂外设置了.value。你可以这样做(Python允许你在几乎任何实例上设置任何属性),但是对每个类都这样做很麻烦。

这是__init__的用武之地。这是一种具有特殊含义的方法(因此是双重下划线)。在这种情况下,它是一个在创建类时调用的方法(当您使用parens调用类来创建新实例时)。 init这里代表initialization:它是一个初始化类属性的函数。创建类时,将需要任何参数(超出self)。

所以,我们可以将我们的例子改为:

class House:
    def __init__(self, value):
        self.value = value # set attribute
    def renovate(self):
        ''' Renovate a house, increasing its value. '''
        self.value += 10000

oldhouse = House(100000) # make a House instance with the value set
mansion = House(10000000) # make another House instance with the value set
oldhouse.renovate()
print oldhouse.value # prints 110000
print mansion.value # prints 10000000

它会有相同的效果,但更好一些(因为我们不必继续在新的.value上设置House)。


* 除了staticmethods和classmethods,以及__new__等特殊情况

相关问题