Python2.7类:尝试运行类时发出的消息

Python2.7类:尝试运行类时发出的消息,python,class,main,nameerror,Python,Class,Main,Nameerror,我已经创建了一个很好的类和两个函数,我想在输入调查名称“mikkel”时运行它们。但当尝试打印“mikkel”调查路线时,我收到以下信息: <__main__.SurveyRoute object at 0x10eb0cf38> 当类运行时,我需要更改什么来打印xcoord和绘图 class SurveyRoute(SurveyPlotWidget): """docstring for SurveyRoute""" def __init__(self, surve

我已经创建了一个很好的类和两个函数,我想在输入调查名称“mikkel”时运行它们。但当尝试打印“mikkel”调查路线时,我收到以下信息:

<__main__.SurveyRoute object at 0x10eb0cf38>
当类运行时,我需要更改什么来打印xcoord和绘图

class SurveyRoute(SurveyPlotWidget):
    """docstring for SurveyRoute"""

    def __init__(self, survey_name):
        self.survey_name = survey_name

    def read_coordinate_file(self, survey_name):
        """
        coords  is a library with 'benchmark nr': UTM X, UTM Y, depth

        The coordinate name should be changed to the coordinate type. Here we are dealing with UTM coordinates
        The coordinate type can be found in the .xls file that contains the coordinates. e.g. mikkel.xls

        df      is the result of using the pandas package to rearrange the coords dictionary.
        """ 
        coords = station_coordinates.get_coordinates_all(survey_name)
        df = pd.DataFrame(coords,index=['UTM X','UTM Y','depth']) 
        df = DataFrame.transpose(df)
        xcoord = df['UTM X'].values.tolist() 
        ycoord = df['UTM Y'].values.tolist()

        print xcoord        

    def plot_coords(xcoord,ycoord):

        fig = plt.figure()
        plt.plot(xcoord, ycoord, marker='o', ms=10, linestyle='', alpha=1.0, color='r', picker = True)[0]
        plt.xlabel('UTM x-coordinate')
        plt.ylabel('UTM y-coordinate')

        x_legend = np.nanmax(xcoord) + 0.01*(np.nanmax(xcoord)-np.nanmin(xcoord))
        y_legend = np.nanmin(ycoord) - 0.01*(np.nanmax(ycoord)-np.nanmin(ycoord))
        map_size = np.sqrt(pow(np.nanmax(xcoord)-np.nanmin(xcoord),2)+pow(np.nanmax(ycoord)-np.nanmin(ycoord),2) )

        #legend_size = 100
        #max_val = np.nanmax(val)
        #if max_val < 50:
        #legend_size = 10
        fig.canvas.mpl_connect('pick_event', on_hover)
        self.canvas.draw()

"""
Set package_directory to the right user (e.g. DJV) and the folder where the station_coordinates are stored.  
"""
package_directory = '/Users/DJV/Desktop/quad-master/station_coordinates'                                     

#survey = SurveyRoute('mikkel')
#print SurveyRoute('mikkel')
#print survey.read_coordinate_file('mikkel') WORKS
#print survey.plot_coords(xcoord,ycoord)  DOESNT WORK

print SurveyRoute('mikkel')
米克尔调查酒店

返回一个对象。所以当你打电话的时候

“米克尔”印刷测量图

您正在打印整个对象。 要在类运行时打印xcoord,请将其放入_uu; init _uuu函数中:

self.read\u坐标\u文件测量\u名称

替换

“米克尔”印刷测量图

米克尔调查酒店

如果执行-SurveyRoute'mikkel',它将创建SurveyRoute类的实例/对象,self.survey_名称等于mikkel。当您尝试打印对象本身时,它会像您观察到的那样打印-

另外,如果plot_coords是一个实例方法,我相信是这样的,因为我可以看到您在函数中使用self,那么这个函数的第一个参数应该是实例self。您应该将其定义为-

def plot_coords(self, xcoord, ycoord):
如果要调用此对象的函数,应将其作为-

obj = SurveyRoute('mikkel')
obj.read_coordinate_file(<parameter survery_name>)
obj.plot_coords(<parameters for xcoord and ycoord>)

这不是错误!!!这就是您打印的类实例!!!你到底想要什么?你希望打印什么?为什么你认为这是一个错误?它没有说错误,也没有给你一个漂亮的红色回溯。我的错。我改变了问题。我想要的是在指定测量名称时绘制xcoord和ycoord的代码。显示xcoord用于检查文件是否正确导入;sr'foo',首先,您必须覆盖surveyroutefi中的_call__方法,感谢您发现self:second。我仍然难以定义。我现在把它们叫做obj.plot_coordsxcoord,ycoord,这显然不起作用。我应该如何定义和/或调用它们?@leermester这就是你调用函数的方式,如果你在定义中加入了self,调用不应该引起任何问题,你是否有任何错误?如果是这样,可能是函数内部的其他错误?我已决定以不同的方式构造代码,以便def plot_coordsself进行绘图。现在尝试从新函数中的前一个函数访问局部变量。不同的问题。