导入单个类并使用该类,得到了意料之外的结果

首先我编写了一个名为“练习9_1.py”的模块,如下

class Restaurant:

    def __init__(self,restaurant_name,cuisine_type):
        self.name=restaurant_name
        self.type=cuisine_type
        print(self.name)
        print(self.type)

    def describe_restaurant(self):
        print(f"The name of the restaurant is {self.name.title()}.")
        print(f"The cuisine type of it is {self.type.title()}.")

    def open_restaurant(self):
        print("The restaurant is opening!")
my_restaurant=Restaurant('gugio', 'japanese food')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
your_restaurant=Restaurant('lucio', 'chinese food')
your_restaurant.describe_restaurant()
your_restaurant.open_restaurant()
his_restaurant=Restaurant('fucci', 'indian food')
his_restaurant.describe_restaurant()
his_restaurant.open_restaurant()

然后我执行了如下代码

from 练习9_1 import Restaurant
my_restaurant=Restaurant('gugio', 'japanese food')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()

结果是:

gugio
japanese food
The name of the restaurant is Gugio.
The cuisine type of it is Japanese Food.
The restaurant is opening!

lucio
chinese food
The name of the restaurant is Lucio.
The cuisine type of it is Chinese Food.
The restaurant is opening!
fucci
indian food
The name of the restaurant is Fucci.
The cuisine type of it is Indian Food.
The restaurant is opening!
gugio
japanese food
The name of the restaurant is Gugio.
The cuisine type of it is Japanese Food.
The restaurant is opening!

我预期中是只会执行下面这部分,为什么还会执行练习9_1模块中Restaurant类后的全部程式码?

my_restaurant=Restaurant('gugio', 'japanese food')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
Jason990420
最佳答案

模块可以包含可执行的语句以及函数定义, 这些语句用于初始化模块. 它们仅在模块第一次在import 语句中被导入时才执行. (当文件被当作脚本运行时,它们也会执行. ). 即使你只导入一部份, 也是要初始化模块.

如果你不想在初始化时, 执行这些语句, 就必须予以区分.

class Restaurant:

    def __init__(self,restaurant_name,cuisine_type):
        self.name=restaurant_name
        self.type=cuisine_type
        print(self.name)
        print(self.type)

    def describe_restaurant(self):
        print(f"The name of the restaurant is {self.name.title()}.")
        print(f"The cuisine type of it is {self.type.title()}.")

    def open_restaurant(self):
        print("The restaurant is opening!")

if __name__ == '__main__':    # 只以脚本的方式执行模块, 才执行
    my_restaurant=Restaurant('gugio', 'japanese food')
    my_restaurant.describe_restaurant()
    my_restaurant.open_restaurant()
    your_restaurant=Restaurant('lucio', 'chinese food')
    your_restaurant.describe_restaurant()
    your_restaurant.open_restaurant()
    his_restaurant=Restaurant('fucci', 'indian food')
    his_restaurant.describe_restaurant()
    his_restaurant.open_restaurant()
2年前 评论
讨论数量: 4
Jason990420

模块可以包含可执行的语句以及函数定义, 这些语句用于初始化模块. 它们仅在模块第一次在import 语句中被导入时才执行. (当文件被当作脚本运行时,它们也会执行. ). 即使你只导入一部份, 也是要初始化模块.

如果你不想在初始化时, 执行这些语句, 就必须予以区分.

class Restaurant:

    def __init__(self,restaurant_name,cuisine_type):
        self.name=restaurant_name
        self.type=cuisine_type
        print(self.name)
        print(self.type)

    def describe_restaurant(self):
        print(f"The name of the restaurant is {self.name.title()}.")
        print(f"The cuisine type of it is {self.type.title()}.")

    def open_restaurant(self):
        print("The restaurant is opening!")

if __name__ == '__main__':    # 只以脚本的方式执行模块, 才执行
    my_restaurant=Restaurant('gugio', 'japanese food')
    my_restaurant.describe_restaurant()
    my_restaurant.open_restaurant()
    your_restaurant=Restaurant('lucio', 'chinese food')
    your_restaurant.describe_restaurant()
    your_restaurant.open_restaurant()
    his_restaurant=Restaurant('fucci', 'indian food')
    his_restaurant.describe_restaurant()
    his_restaurant.open_restaurant()
2年前 评论

@Jason990420 请问 “if name == 'main': ”这条语句是什么意思?起的作用是?

2年前 评论
Jason990420

__name__ 是当前模块名,当模块被直接运行时模块名为 '__main__' 。这句话的意思就是,当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

2年前 评论
Jason990420

为什么需要选择最佳答案?

问题解决后选择「最佳答案」,或者自己回帖并标记为「最佳答案」:

  • 对于尝试帮助你的人,他们能感觉某种满足感,因为这有时能让他们也感觉学到东西;
  • 满足感对于专家、高手和黑客来讲是一种很重要的驱动力;
  • 做事有始有终,积攒人品,让人觉得你值得帮忙,下次你会获得更多的帮助;
  • 后续自己也方便查阅,其他搜索到此问题的同行,也能够快速得知哪个才是正确答案。

另外,考虑一下怎样才能避免他人将来也遇到类似的问题,问问自己编一份文档或 FAQ 补丁会不会有帮助,如果是的话就将补丁发给维护者。在黑客中,这种良好的后继行动实际上比传统的礼貌更重要,也是你善待他人而赢得声誉的方式,这是非常有价值的财富。

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!