在 Python 中创建对象时使用 self
Use of self when creating objects in Python
我做了几个类,都是做同样的事情,但我还是不完全理解其中的区别,哪个最好用。此外,第四个对象不工作。它说'NameError: name 'self' is not defined',虽然我不明白出了什么问题。这是我写的,输出是7,7,7,0:
class addTwoNumbers1(object):
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
self.result = number1 + number2
class addTwoNumbers2(object):
def __init__(self, number1, number2):
self.result = number1 + number2
class addTwoNumbers3(object):
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
def Add(self):
result = self.number1 + self.number2
return result
class addTwoNumbers4(object):
result = 0
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
result = self.number1 + self.number2
# Test classes for adding two numbers:
addingObject1 = addTwoNumbers1(5,2)
print addingObject1.result
addingObject2 = addTwoNumbers2(5,2)
print addingObject2.result
addingObject3 = addTwoNumbers3(5,2)
print addingObject3.Add()
addingObject4 = addTwoNumbers4(5,2)
print addingObject4.result
提前感谢您的任何建议。
好吧,您的 classes 看起来应该是一个函数,但我假设这只是示例。话虽如此
当您的对象需要存储构造它的值以及由它们产生的值时,这很有用:
class addTwoNumbers1(object):
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
self.result = number1 + number2
当您的对象需要存储的不是构造它的值,而只是它们的结果时,这很有用:
class addTwoNumbers2(object):
def __init__(self, number1, number2):
self.result = number1 + number2
当您的对象只需要存储构造它的值时,这很有用。结果应该是按需计算的,不是对象状态的一部分。
class addTwoNumbers3(object):
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
def Add(self):
result = self.number1 + self.number2
return result
没有。没有意义。这是一个 class 变量。
class addTwoNumbers4(对象):
result = 0
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
result = self.number1 + self.number2
我做了几个类,都是做同样的事情,但我还是不完全理解其中的区别,哪个最好用。此外,第四个对象不工作。它说'NameError: name 'self' is not defined',虽然我不明白出了什么问题。这是我写的,输出是7,7,7,0:
class addTwoNumbers1(object):
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
self.result = number1 + number2
class addTwoNumbers2(object):
def __init__(self, number1, number2):
self.result = number1 + number2
class addTwoNumbers3(object):
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
def Add(self):
result = self.number1 + self.number2
return result
class addTwoNumbers4(object):
result = 0
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
result = self.number1 + self.number2
# Test classes for adding two numbers:
addingObject1 = addTwoNumbers1(5,2)
print addingObject1.result
addingObject2 = addTwoNumbers2(5,2)
print addingObject2.result
addingObject3 = addTwoNumbers3(5,2)
print addingObject3.Add()
addingObject4 = addTwoNumbers4(5,2)
print addingObject4.result
提前感谢您的任何建议。
好吧,您的 classes 看起来应该是一个函数,但我假设这只是示例。话虽如此
当您的对象需要存储构造它的值以及由它们产生的值时,这很有用:
class addTwoNumbers1(object):
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
self.result = number1 + number2
当您的对象需要存储的不是构造它的值,而只是它们的结果时,这很有用:
class addTwoNumbers2(object):
def __init__(self, number1, number2):
self.result = number1 + number2
当您的对象只需要存储构造它的值时,这很有用。结果应该是按需计算的,不是对象状态的一部分。
class addTwoNumbers3(object):
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
def Add(self):
result = self.number1 + self.number2
return result
没有。没有意义。这是一个 class 变量。
class addTwoNumbers4(对象):
result = 0
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
result = self.number1 + self.number2