如何在Python中声明全局变量?

时间:2017-08-01 03:32:57

标签: python python-3.x global-variables

我班上有一个属性

class MITCampus(Campus):
    """ A MITCampus is a Campus that contains tents """
    def __init__(self, center_loc, tent_loc = Location(0,0)): 

        """ Assumes center_loc and tent_loc are Location objects 
        Initializes a new Campus centered at location center_loc 
        with a tent at location tent_loc """

        Campus.__init__(self,center_loc)



    def add_tent(self, new_tent_loc):
        """ Assumes new_tent_loc is a Location
        Adds new_tent_loc to the campus only if the tent is at least 0.5 distance 
        away from all other tents already there. Campus is unchanged otherwise.
        Returns True if it could add the tent, False otherwise. """
        # Your code here

为了跟踪位置,我创建了一个名为tent_list = []的新变量。但问题是,无论何时创建MITCampus的新实例并调用add_tent,tent_list都会重置为空列表。

  

请帮助!!

0 个答案:

没有答案
相关问题