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

2024-04-19 04:49:27 发布

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

我的类中有一个属性

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=[]的新变量。但问题是,只要创建了一个新的mitcumpus实例,并且add\u tent被称为tent,tent\u list就会重置为空list。你知道吗

Please help!!


Tags: theselfaddnewisdeflocationloc