(问答题)
用类描述一下你的梦中情人:至少包含名字、长相、身体,以及它的爱好
前段时间有幸参加了华为昇腾CANN训练营第二期,说实话,也没怎么听,一是那几天确实忙,二是也确实听不懂。
在安卓应用层开发多年,听到什么机器学习、卷积甚至python就头大,以前还和同学开玩笑说这辈子不会碰python,没想到这么快就打脸。
至于为什么要搞这玩意儿,其实跟研究生课题有关,没办法,导师说搞,那就搞呗,印证了那句歌词怎么唱来着,什么“被逼的,被逼的”,哈哈,开个玩笑。
好了,废话不多说。
这两天又重新把视频看了一遍,目的很简单,就是昇腾的几个概论搞清楚,弄清楚自己究竟是在干啥,自己写的代码在整个生态中处于啥地位。
class DreamLover:
def __init__(self, name, appearance, body, hobbies):
self.name = name
self.appearance = appearance
self.body = body
self.hobbies = hobbies
def describe_hobbies(self):
for hobby, details in self.hobbies.items():
print(hobby + ":")
for detail in details:
print("- " + detail)
# Create an instance of DreamLover with desired attributes
lover = DreamLover("John", "Handsome and charming", "Fit and athletic", {
"Reading": ["Fiction novels", "Self-help books"],
"Outdoor activities": ["Hiking", "Camping"],
"Music": ["Playing guitar", "Listening to jazz"]
})
# Output the description of hobbies
lover.describe_hobbies()
The code above defines a class called DreamLover
with attributes for name, appearance, body, and hobbies. The constructor __init__
initializes these attributes with the given values.
The method describe_hobbies
prints out the hobbies and their associated details using a for loop to iterate over the hobbies dictionary. Each hobby is printed followed by a list of details using nested for loops.
To create an instance of DreamLover
with desired attributes, you can pass the values to the constructor. In the example, an instance named lover
is created with name "John", appearance "Handsome and charming", body "Fit and athletic", and hobbies dictionary containing different hobbies with their details.
Finally, the describe_hobbies
method is called on the lover
instance to print out the description of hobbies. Output will show each hobby followed by a list of details for that hobby.
class DreamLover:
def __init__(self, name, appearance, height, hobby):
self.name = name
self.appearance = appearance
self.height = height
self.hobby = hobby