[英]__init__ Python Syntax
I created a class for Food items. 我为食品项目创建了一个类。 I'm wondering why in
def __init__(self, type)
there are two arguments but when I call Food('Meat')
I only pass one argument. 我想知道为什么在
def __init__(self, type)
有两个参数,但是当我调用Food('Meat')
我只传递了一个参数。 I assume this is because __init__
is special somehow? 我认为这是因为
__init__
有点特殊吗?
class Food(object):
def __init__(self, type):
self.type = type
def cook_food_item(self):
if self.type == 'Meat':
print 'Grill it!'
elif self.type == 'Vegetable':
print 'Boil it!'
elif self.type == 'Dairy':
print 'Churn it!'
hot_dog = Food('Meat')
hot_dog.cook_food_item()
__init__
is special indeed because it's a magic method , although that's not what makes it receive self
as a first parameter. __init__
确实很特别,因为它是一种魔术方法 ,尽管这并不是使它接受self
作为第一个参数的原因。 Being a class method does that. 作为类方法可以做到这一点。 Every method receives
self
as a first argument, which is passed automatically by Python. 每个方法都将
self
作为第一个参数,由Python自动传递。 I strongly recommend you read the Python basics, like classes , in this case. 在这种情况下,我强烈建议您阅读Python基础知识,例如class 。
All methods in a class pass the class instance as their first argument, including __init__
. 类中的所有方法都将类实例作为其第一个参数传递,包括
__init__
。 This is true for the other method in your class as well: def cook_food_item(self)
. 对于您的类中的其他方法也是如此:
def cook_food_item(self)
。
If you had another method that took an actual argument, you'd put that argument after self
, leaving something like: 如果您有另一个采用实际参数的方法,则可以将该参数放在
self
后面,并保留以下内容:
class Food(object):
...
def eat_with(self, drink):
print("Eating a " + self.type + " while drinking a " + drink)
>>> hot_dog = Food("hot dog")
>>> hot_dog.eat_with("coke")
Eating a hot dog while drinking a coke
Behind the scenes this is doing something kind of like 在幕后,这就像
>>> hot_dog.eat_with("coke")
# becomes something resembling
hot_dog.__class__.eat_with(hot_dog, "coke") # hot_dog.__class__ is Food
There's nothing special about __init__
here. 这里的
__init__
没有什么特别的。
In Python, you have two kinds of methods: bound and unbound. 在Python中,您有两种方法:绑定方法和非绑定方法。 Methods defined in a class are usually bound, which let you write
myobj.mymethod()
instead of MyClass.mymethod(myobj)
. 通常,绑定在类中定义的方法,这使您可以编写
myobj.mymethod()
而不是MyClass.mymethod(myobj)
。 Unbound methods have no self
argument and act just like regular functions. 未绑定方法没有
self
变量,其行为与常规函数一样。
To illustrate the point, you can create an unbound method like this: 为了说明这一点,您可以创建一个未绑定的方法,如下所示:
def myfunction(x):
return x
class MyClass(object):
def __init__(self):
self.unbound = myfunction
def bound(self, x):
print('MyClass.bound called with', self)
return x
myobj = MyClass()
print(myobj.unbound(42))
print(myobj.bound(42))
Note that myfunction
(and therefore myobj.unbound
) have no access to self
unless you explicitly pass it in. So in general, bound methods like MyClass.bound
are the tool of choice when writing classes. 请注意,除非您显式传递
myfunction
(因此, myobj.unbound
)无法访问self
。因此,通常,诸如MyClass.bound
类的绑定方法是编写类时的首选工具。 __init__
is no different. __init__
没什么不同。 It wouldn't be very useful as an unbound method. 作为未绑定方法,它不是很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.