[英]Python OOP - calling a methode in a class
I am new in Python OOP and I am trying to understand one line of this Code (This is just a part of the whole Code)我是 Python OOP 的新手,我试图理解这段代码的一行(这只是整个代码的一部分)
I am trying to understand what "pet.name" in the methode "whichone" do.我试图了解方法“whichone”中的“pet.name”是做什么的。 The parameter 'petlist' in whichone can be empty or a list with strings.参数 'petlist' 可以是空的,也可以是带有字符串的列表。 The Parameter 'name' has just one string.参数“名称”只有一个字符串。
Can someone explain me what "pet.name" actually do?有人可以解释一下“pet.name”实际上是做什么的吗?
from random import randrange
class Pet():
boredom_decrement = 4
hunger_decrement = 6
boredom_threshold = 5
hunger_threshold = 10
sounds = ['Mrrp']
def __init__(self, name = "Kitty"):
self.name = name
self.hunger = randrange(self.hunger_threshold)
self.boredom = randrange(self.boredom_threshold)
self.sounds = self.sounds[:]
def whichone(petlist, name):
for pet in petlist:
if pet.name == name:
return pet
return None # no pet matched
def play():
animals = []
option = ""
base_prompt = """
Quit
Choice: """
feedback = ""
while True:
action = input(feedback + "\n" + base_prompt)
feedback = ""
words = action.split()
if len(words) > 0:
command = words[0]
else:
command = None
if command == "Quit":
print("Exiting...")
return
elif command == "Adopt" and len(words) > 1:
if whichone(animals, words[1]):
feedback += "You already have a pet with that name\n"
else:
animals.append(Pet(words[1]))
play()
Each Pet
has a name that you give when constructing an object, that being pet.name
("Kitty" by default).每个Pet
都有一个您在构造对象时指定的名称,即pet.name
(默认为“Kitty”)。
I assume whichdone()
is supposed to get a list of Pet
objects and query them against name
argument.我假设whichdone()
应该获取Pet
对象的列表并根据name
参数查询它们。 It will return the first Pet
whose name matches a name
that you've used as input to whichdone()
.它将返回name
与您用作whichdone()
输入的名称匹配的第一个Pet
。
Assuming the petlist
parameter is a List
of Pet
objects, then the for pet in petlist
line will iterate through the list and you will be able to use the pet
variable to access the current element.假设petlist
参数是一个Pet
对象List
,那么for pet in petlist
行将遍历列表,您将能够使用pet
变量访问当前元素。
What is happening in the for loop is that you check whether the current object has the name you passed as a second parameter of the whichone
function. for 循环中发生的事情是检查当前对象是否具有作为whichone
函数的第二个参数传递的名称。 To do this, you'll need to access the name
attribute of the object stored in the pet
variable (which is assumed to be of type Pet
).为此,您需要访问存储在pet
变量(假定为Pet
类型)中的对象的name
属性。 The Python syntax for this is pet.name
( <variable_name>.<attribute_name>
). Python 语法是pet.name
( <variable_name>.<attribute_name>
)。
You know of the existence of this attribute thanks to the class definition, to the __init__
method, where you can see the new instance of the Pet class will receive a name upon creation (which is defaulted to "Kitty").由于类定义和__init__
方法,您知道此属性的存在,您可以在其中看到 Pet 类的新实例将在创建时收到一个名称(默认为“Kitty”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.