我正在学习Python编程语言,我遇到了一些我不完全理解的东西。
在如下方法中:
def method(self, blah):
def __init__(?):
....
....
self做什么?这意味着什么?这是强制性的吗?
__init__方法是做什么的?为什么有必要?(等)
我认为它们可能是面向对象结构,但我知道的不多。
我正在学习Python编程语言,我遇到了一些我不完全理解的东西。
在如下方法中:
def method(self, blah):
def __init__(?):
....
....
self做什么?这意味着什么?这是强制性的吗?
__init__方法是做什么的?为什么有必要?(等)
我认为它们可能是面向对象结构,但我知道的不多。
当前回答
类对象支持两种操作:属性引用和实例化
属性引用使用Python中所有属性引用使用的标准语法:obj.name。有效属性名是创建类对象时类名称空间中的所有名称。所以,如果类定义是这样的:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
然后MyClass。i和MyClass。F是有效的属性引用,分别返回一个整数和一个函数对象。类属性也可以被赋值,所以你可以改变MyClass的值。我被指派。__doc__也是一个有效的属性,返回属于类的文档字符串:" a simple example class"。
类实例化使用函数表示法。假设类对象是一个返回类的新实例的无参数函数。例如:
x = MyClass()
实例化操作(“调用”类对象)创建一个空对象。许多类喜欢用自定义为特定初始状态的实例创建对象。因此,类可以定义一个名为__init__()的特殊方法,如下所示:
def __init__(self):
self.data = []
当类定义__init__()方法时,类实例化会自动为新创建的类实例调用__init__()。所以在这个例子中,一个新的,初始化的实例可以通过:
x = MyClass()
当然,__init__()方法可能有参数以获得更大的灵活性。在这种情况下,给类实例化操作符的参数被传递给__init__()。例如,
class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart
x = Complex(3.0, -4.5)
x.r, x.i
这是从官方文件中截取的,最终对我帮助最大。
这是我的例子
class Bill():
def __init__(self,apples,figs,dates):
self.apples = apples
self.figs = figs
self.dates = dates
self.bill = apples + figs + dates
print ("Buy",self.apples,"apples", self.figs,"figs
and",self.dates,"dates.
Total fruitty bill is",self.bill," pieces of fruit :)")
创建类Bill的实例时:
purchase = Bill(5,6,7)
你会得到:
> Buy 5 apples 6 figs and 7 dates. Total fruitty bill is 18 pieces of
> fruit :)
其他回答
在这段代码中:
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
... self变量表示对象本身的实例。大多数面向对象语言将此作为一个隐藏参数传递给对象上定义的方法;Python则不然。你必须显式地声明它。当你创建一个A类的实例并调用它的方法时,它将被自动传递,就像在…
a = A() # We do not pass any argument to the __init__ method
a.method_a('Sailor!') # We only pass a single argument
__init__方法大致代表Python中的构造函数。当你调用A()时,Python为你创建一个对象,并将其作为第一个参数传递给__init__方法。任何额外的参数(例如,A(24, 'Hello'))也会作为参数传递——在这种情况下会引发异常,因为构造函数并不期望它们。
注意,self实际上可以是任何有效的python标识符。例如,从Chris B的例子中,我们可以简单地写:
class A(object):
def __init__(foo):
foo.x = 'Hello'
def method_a(bar, foo):
print bar.x + ' ' + foo
结果是一样的。然而,建议使用self,因为其他python更容易识别它。
类对象支持两种操作:属性引用和实例化
属性引用使用Python中所有属性引用使用的标准语法:obj.name。有效属性名是创建类对象时类名称空间中的所有名称。所以,如果类定义是这样的:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
然后MyClass。i和MyClass。F是有效的属性引用,分别返回一个整数和一个函数对象。类属性也可以被赋值,所以你可以改变MyClass的值。我被指派。__doc__也是一个有效的属性,返回属于类的文档字符串:" a simple example class"。
类实例化使用函数表示法。假设类对象是一个返回类的新实例的无参数函数。例如:
x = MyClass()
实例化操作(“调用”类对象)创建一个空对象。许多类喜欢用自定义为特定初始状态的实例创建对象。因此,类可以定义一个名为__init__()的特殊方法,如下所示:
def __init__(self):
self.data = []
当类定义__init__()方法时,类实例化会自动为新创建的类实例调用__init__()。所以在这个例子中,一个新的,初始化的实例可以通过:
x = MyClass()
当然,__init__()方法可能有参数以获得更大的灵活性。在这种情况下,给类实例化操作符的参数被传递给__init__()。例如,
class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart
x = Complex(3.0, -4.5)
x.r, x.i
这是从官方文件中截取的,最终对我帮助最大。
这是我的例子
class Bill():
def __init__(self,apples,figs,dates):
self.apples = apples
self.figs = figs
self.dates = dates
self.bill = apples + figs + dates
print ("Buy",self.apples,"apples", self.figs,"figs
and",self.dates,"dates.
Total fruitty bill is",self.bill," pieces of fruit :)")
创建类Bill的实例时:
purchase = Bill(5,6,7)
你会得到:
> Buy 5 apples 6 figs and 7 dates. Total fruitty bill is 18 pieces of
> fruit :)
在这里,这个家伙写得很好,很简单:https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
阅读上面的链接作为参考:
self? So what's with that self parameter to all of the Customer methods? What is it? Why, it's the instance, of course! Put another way, a method like withdraw defines the instructions for withdrawing money from some abstract customer's account. Calling jeff.withdraw(100.0) puts those instructions to use on the jeff instance. So when we say def withdraw(self, amount):, we're saying, "here's how you withdraw money from a Customer object (which we'll call self) and a dollar figure (which we'll call amount). self is the instance of the Customer that withdraw is being called on. That's not me making analogies, either. jeff.withdraw(100.0) is just shorthand for Customer.withdraw(jeff, 100.0), which is perfectly valid (if not often seen) code. init self may make sense for other methods, but what about init? When we call init, we're in the process of creating an object, so how can there already be a self? Python allows us to extend the self pattern to when objects are constructed as well, even though it doesn't exactly fit. Just imagine that jeff = Customer('Jeff Knupp', 1000.0) is the same as calling jeff = Customer(jeff, 'Jeff Knupp', 1000.0); the jeff that's passed in is also made the result. This is why when we call init, we initialize objects by saying things like self.name = name. Remember, since self is the instance, this is equivalent to saying jeff.name = name, which is the same as jeff.name = 'Jeff Knupp. Similarly, self.balance = balance is the same as jeff.balance = 1000.0. After these two lines, we consider the Customer object "initialized" and ready for use. Be careful what you __init__ After init has finished, the caller can rightly assume that the object is ready to use. That is, after jeff = Customer('Jeff Knupp', 1000.0), we can start making deposit and withdraw calls on jeff; jeff is a fully-initialized object.
基本上,当在同一个类中的多个函数中使用一个变量时,需要使用'self'关键字。至于init,它用于设置默认值,以防该类中没有其他函数被调用。