如何在Sublime文本3中将Python(3.81)与Anaconda一起使用

2024-06-12 01:27:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我当时正在使用Sublime Text 3编写Python速成教程中的一个练习,该练习涉及父类和子类,我在尝试构建/运行程序时遇到了super()函数的问题

我正在使用Conda(Anaconda)构建系统来构建我的程序。当我使用Conda运行程序时,super()函数出现回溯错误:

Traceback (most recent call last):
  File "/Users/watchmen1/Desktop/python_work/Chapter 9/ice_cream_stand.py", line 61, in <module>
    braums = IceCreamStand('Braums')
  File "/Users/watchmen1/Desktop/python_work/Chapter 9/ice_cream_stand.py", line 52, in __init__
    super().__init__(restaurant_name, cuisine_type)
TypeError: super() takes at least 1 argument (0 given)
[Finished in 0.0s with exit code 1]
[cmd: ['python', '-u', '/Users/watchmen1/Desktop/python_work/Chapter 9/ice_cream_stand.py']]
[dir: /Users/watchmen1/Desktop/python_work/Chapter 9]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

但是,当我仅使用常规的Python3构建系统运行它时,我不会遇到这个问题,我的理解是,至少在Python3中,super()函数在使用它连接父类和子类时不需要参数

当我将Anaconda用作构建系统时,我假设它是以Python 3的形式运行的。然而,由于错误似乎是说我需要输入一个参数,我想知道它是否实际作为Python2.7或其他什么运行,因为我的书指出,在Python2.7中,super()函数确实需要参数

我可以使用Python3作为构建系统,但考虑到我计划在将来使用Anaconda,我希望我能弄清楚到底发生了什么

class Restaurant():
    """ An attempt to model a restaurant."""

    def __init__(self, restaurant_name, cuisine_type):
        """Initialize restaurant name and cuisine type."""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0

    def describe_restaurant(self):
        """Simulate a sentance naming the restaurant and giving its cuisine type."""
        print(self.restaurant_name.title() + " is a restaurant that serves " + self.cuisine_type.title() + " food.")

    def open_restaurant(self):
        """Simulate a sentance announcing the restaurant is open."""
        print(self.restaurant_name.title() + ", is now open!")

    def set_number_served(self, customers_served):
        """Set the number of people served at a #restaurant."""
        self.number_served = customers_served


    def increment_number_served(self, customers):
        """Add selected amount of customers to the total number served."""
        self.number_served += customers


# restaurant = Restaurant('Shake Shack', 'Americana')

#print(restaurant.resturant_name.title())
#print(restaurant.cuisine_type.title())

# restaurant.describe_resturant()
# #print("")
# restaurant.open_resturant()
# print("\nNumber Served: " + str(restaurant.number_served))
# print("")

# #restaurant.number_served = 20
# restaurant.set_number_served(20)
# print("Number Served: " + str(restaurant.number_served))

# print('')

# restaurant.increment_number_served(22)
# print("Number Served: " + str(restaurant.number_served))

class IceCreamStand(Restaurant):
    """Create child class of restaurant that represents an Ice Cream Stand."""
    def __init__(self, restaurant_name, cuisine_type='ice_cream'):
        """Initialize attributes of the parent class."""
        super().__init__(restaurant_name, cuisine_type)
        self.flavors = []

    def show_flavors(self):
        """Display the list of flavors the Ice Cream Stand offers."""
        print("\nThis Ice Cream Stand offers the following flavors: ")
        for flavor in self.flavors:
            print("\t- " + flavor.title())

braums = IceCreamStand('Braums')
braums.flavors = ['Chocolate', 'Vanilla', 'Sherbert']

braums.describe_restaurant()
braums.show_flavors()

Tags: thenameselfnumbertitleinitdeftype