2014-10-07 125 views
0

我試圖填充從chart_title像這樣的內容我的標題變量:填充類變量與另一個類的變量的值

class LineHighChart(object): 
    chart_title = '' 

    title = {'text':chart_title, 
      'x':-20 
      } 

我通過這樣做在另一:

def weight_graph(request): 
    highchart = LineHighChart() 

    chart_title = 'my chart title' 


    highchart.chart_title = chart_title 

    print highchart.__dict__ 

    return JsonResponse(highchart.__dict__, safe=False) 

這不包含填充title屬性,而是打印:

{'chart_title': 'joe'} 

我希望它爲p rint:

{'title': {'text':'my chart title', 
      'x':-20 
      }} 

回答

0

我認爲更好的方式來充分利用基於組的setter值變化的實例變量將要做到以下幾點:

class LineHighChart(object): 
    #title = {'text':' ', 'x':-20} 


    def __init__(self, title, subtitle, x_axis_list, y_axis_title, metric, series): 

     self.title = {'text': title, 'x':-20} 

     self.subtitle = { 
      'text': subtitle, 
      'x': -20 
     } 

     self.xAxis = { 
      'categories': x_axis_list 
     } 

     self.yAxis = { 
      'title': { 
       'text':y_axis_title 
      }, 
      'plotLines': [{ 
       'value': 0, 
       'width': 1, 
       'color': '#808080' 
      }] 
     } 

     self.tooltip = { 
      'valueSuffix': metric 
     } 

     self.legend = { 
      'layout': 'vertical', 
      'align': 'right', 
      'verticalAlign': 'middle', 
      'borderWidth': 0 
     } 

     self.series = series 

然後調用這個類如:

def weight_graph(request): 
    print 'weight graph' 
    x_axis_list = ['Day 1', 'Day 2', 'Day 3'] 
    series = [{ 
      'name': 'Tokyo', 
      'data': [7.0, 1.0, 9.5] 
     }, { 
      'name': 'New York', 
      'data': [-0.2, 0.8, 5.7] 
     }] 

    highchart = LineHighChart(
           'my chart', 
           'last seven days', 
           x_axis_list, 
           'Weight', 
           ' lbs', 
           series 
          ) 

    return JsonResponse(highchart.__dict__, safe=False) 
0

是的,LineHighChart中的標題字典不會自動更新,因爲您更新了對象的另一個屬性。

class LineHighChart(object): 
    def __init__(self): 
     self._chart_title = '' 
     self.title = {'text': self._chart_title, 'x':-20} 

    @property 
    def chart_title(self): 
     return self._chart_title 

    @chart_title.setter 
    def chart_title(self, value): 
     self._chart_title = value 
     self.title['text'] = value 

highchart = LineHighChart() 
chart_title = 'my chart title' 
highchart.chart_title = chart_title 
print highchart.__dict__ 
# {'_chart_title': 'my chart title', 'title': {'text': 'my chart title', 'x': -20}} 

我也改變了你的類變量(class.variable由該類的所有實例共享)實例變量(self.variable),以便當您更新chart_title中:如果你想獲得這種行爲,你可以使用一個setter屬性一個對象不會影響同一類的其他實例。

+0

這樣設置'chart_title'爲一個實例會影響所有的實例。我不認爲這是所希望的,更好地移動'__init__'中的前兩個聲明。 – 2014-10-07 23:07:22

+0

哦,你是對的,沒有注意到他有班級變數。現在應該可以。 – elyase 2014-10-07 23:13:45

+0

我得到的錯誤:LineHighChart'對象沒有屬性'標題' – Atma 2014-10-07 23:44:02

0

你定義了幾種,不會出現在實例中__dict__

class LineHighChart(object): 
    chart_title = '' 
    title = {'text':chart_title, 
      'x':-20 
      } 

您然後用相同的名稱作爲類變量

highchart = LineHighChart() 
chart_title = 'my chart title' 
highchart.chart_title = chart_title 

儘管名稱定義實例級別的變量類級變量是相同的,這兩個變量存在於不同的名稱空間中。當json對實例級別名稱空間進行編碼時,不包括類級別的任何內容。

如果你想實例級的屬性,它們包括在初始化類

class LineHighChart(object): 

    def __init__(self): 
     self.chart_title = '' 
     self.title = {'text':self.chart_title, 
      'x':-20 
      } 

但也考慮到self.title['text']當一個新的值分配給self.chart_title不會改變。

0

隨着對類變量標題會談...

class LineHighChart(object): 
    chart_title = 'Class Variable' 

    # using a property getter for the title, to be dynamic 
    @property 
    def title(self): 
     # uses the class variable 
     return {'text': LineHighChart.chart_title, 'x': -20} 

    def __init__(self): 
     # gets initialized and will not be changed 
     self.title2 = {'text': self.chart_title, 'x': -20} 

def weight_graph(request): 
    highchart = LineHighChart() 
    lowchart = LineHighChart() 

    # setting a NEW object variable 
    highchart.chart_title = 'new object variable value' 

    # overriding the class variable 
    LineHighChart.chart_title = 'new Class Variable' 

    print('class variable:  {}'.format(LineHighChart.chart_title)) 

    # dict does not contain class variables 
    print('highchart.__dict__: {}'.format(highchart.__dict__)) 

    # get the NEW object variable! 
    print('highchart.chart_title: {}'.format(highchart.chart_title)) 
    # title uses the class variable 
    print('highchart.title:  {}'.format(highchart.title)) 
    # title2 uses the 'old' class variable 
    print('highchart.title2:  {}'.format(highchart.title2)) 

    # get the class variable, because no new object variable was set 
    print('lowchart.chart_title: {}'.format(lowchart.chart_title)) 
    # title uses the class variable 
    print('lowchart.title:  {}'.format(lowchart.title)) 
    # title2 uses the 'old' class variable 
    print('lowchart.title2:  {}'.format(lowchart.title2)) 

weight_graph(None) 

,輸出是:

class variable:  new Class Variable 
highchart.__dict__: {'title2': {'x': -20, 'text': 'Class Variable'}, 'chart_title': 'new object variable value'} 

highchart.chart_title: new object variable value 
highchart.title:  {'x': -20, 'text': 'new Class Variable'} 
highchart.title2:  {'x': -20, 'text': 'Class Variable'} 

lowchart.chart_title: new Class Variable 
lowchart.title:  {'x': -20, 'text': 'new Class Variable'} 
lowchart.title2:  {'x': -20, 'text': 'Class Variable'}