2015-02-06 22 views
0

我在Python中的類結構內創建了一個類。最後,我嘗試檢索其中一個屬性(price)到sum的所有值的列表,並使用它進行數學運算。訪問和列出Python類中的屬性

它一直告訴我,我的班級TOOLBOX或我的班級DATA都沒有屬性Price。我怎麼能解決這個問題?

我的代碼如下所示:

class DATA: 
    def __init__(self, Identifier, Price, Date, Postcode, Type, Age, Tenure, Primary, Secondary, Street, Locality, Town, District, County, Status): 
     self.Identifier = Identifier 
     self.Price = Price 
     self.Date = Date 
     self.Postcode = Postcode 
     self.Type = Type 
     self.Age = Age 
     self.Tenure = Tenure 
     self.Primary = Primary 
     self.Secondary = Secondary 
     self.Street = Street 
     self.Locality = Locality 
     self.Town = Town 
     self.District = District 
     self.County = County 
     self.Status = Status 

class TOOLBOX(object): 

    def __init__ (self): 
     self.alldata = [] 

    def add_data(self, Identifier, Price, Date, Postcode, Type, Time, Tenure, Primary, Secondary, Street, Locality, Town, District, County, Status): 
     self.alldata.append(DATA(Identifier, Price, Date, Postcode, Type, Time, Tenure, Primary, Secondary, Street, Locality, Town, District, County, Status)) 

    def get_prize(self) : 
     price=[] 
     for line in self.alldata: 
       price.append(self.alldata.Price) 
     print price 

    def summation(self): 
     return sum(self.alldata.Price) 



csv_ff = csv.reader(open("FINAL.csv",'rU')) 
l=len(list(csv.reader(open("FINAL.csv",'rU')))) 

dd = TOOLBOX() 

for line in csv_ff: 
    if len(line)==15: 

     Identifier=line[0] 
     Price=int(line[1]) 
     Date=line[2] 
     Postcode=line[3] 
     Type=line[4] 
     Age=line[5] 
     Tenure=line[6] 
     Primary=line[7] 
     Secondary=line[8] 
     Street=line[9] 
     Locality=line[10] 
     Town=line[11] 
     District=line[12] 
     County=line[13] 
     Status=line[14] 

     dd.add_data(Identifier, Price, Date, Postcode, Type, Age, Tenure, Primary, Secondary, Street, Locality, Town, District, County, Status) 
+2

不應該是'price.append(line.Price)'? – jonrsharpe 2015-02-06 11:14:40

回答

0

您正在試圖使用你ALLDATA列表訪問屬性:

self.alldata.Price # incorrect 

self.Price # correct 

self.Price是在DATA類的屬性,self.alldata是一個列表包含DATA實例,您需要遍歷列表並調用類似ele.Price的內容來訪問ele爲for ele in self.alldata的屬性:....

你基本上試圖在列表上做[].Price,一個列表當然沒有Price方法,所以這會失敗。

def get_prize(self) : 
     price = [] 
     for line in self.alldata: # stores instances of DATA 
       price.append(line.Price) # access attribute using the instance 
     print price 

def summation(self): 
    return sum(inst.Price for inst in self.alldata) # again access using instance 

如果你改變了get_prize方法返回價格列表:

 def get_prize(self) : 
      price = [] 
      for line in self.alldata: # stores instances of DATA 
        price.append(line.Price) # access attribute using the instamce 
      return price 

我們可以簡單地使用該方法來總結:

def summation(self): 
     return sum(self.get_prize()) 

我們也可以返回一個列表理解在get_prize這是一個更簡潔一點,並使用更多描述每個元素是什麼變量:

def get_prize(self) : 
    return [inst.Price for inst in self.alldata] 

在一個側面說明使用小寫字母和下劃線的屬性等。self.price, identifier self.all_data ...

+0

非常感謝你們倆。我幾乎呆了一整天,現在它工作:)。我只是剛剛開始編程,這可能是一個概念上的缺陷,但爲什麼我的DATA類沒有顯示我在定義dir(DATA)時定義的所有屬性?我只是得到這個: >>> dir(DATA) ['__doc__','__init__','__module__'] – 2015-02-06 11:40:42

+0

你是指'dr = DATA(1,1,1,1,1,1,1, 11,1,1,11,1,1,1,1)打印(DIR(DR))'?您需要先創建一個實例 – 2015-02-06 11:43:09

0

你類DATA確實有屬性 - 什麼沒有價格屬性是ATTR。 alldataTOOLBOX。它只是一個列表。

雖然在Python中構建一個可能以這種「jQueriest」方式行爲的序列對象是可能的,簡單且有趣的 - 即:在想從序列中獲取屬性時,檢索所有屬性的序列對象,但不是該語言的默認行爲 - 如果沒有專門的類,則執行toolbox_object.alldata.Price會嘗試檢索alldataPrice屬性。

檢索所有的價格序列(如發電機表達式),你應該寫:

(elem.Price for elem in toolbox_object.alldata) 

因此,你的行雲:

def summation(self): 
    return sum(self.alldata.Price) 

你應該有:

def summation(self): 
    return sum(element.Price for element in self.alldata) 

(順便說一句,你的DATA類也應該繼承「object」)

現在,爲了好玩,如果你想要類似jQuery的屬性檢索,你可以創建一個派生自Python列表的方法,只需要額外的__getattr__方法 - 應該可以工作(真正的「本書」,「產品就緒」事情可以用抽象基類來實現,等等 - 但是這只是工作):

class JQList(list): 
    def __getattr__(self, attr): 
      return [getattr(element, attr) for element in self] 

>>> import random 
>>> 
>>> b = JQList() 
>>> b = JQList(random.randint(0,10) + random.randint(0,10) * 1j for i in range(10)) 
>>> b 
[(5+7j), (3+4j), 3j, (4+9j), (1+9j), (2+3j), (1+0j), (10+4j), 9j, (9+10j)] 
>>> b.real 
[5.0, 3.0, 0.0, 4.0, 1.0, 2.0, 1.0, 10.0, 0.0, 9.0] 
>>> b.imag 
[7.0, 4.0, 3.0, 9.0, 9.0, 3.0, 0.0, 4.0, 9.0, 10.0] 
1

Price是存儲在該self.alldata列表中DATA實例的attibute。因此,您需要遍歷self.alldata列表,抓住Price屬性,像這樣:

def get_prices(self) : 
    prices=[] 
    for line in self.alldata: 
     price.append(line.Price) 
    return prices 

注:

  1. 我改名從get_prize的方法get_prices
  2. get_prices()現在回報列表的價格,而不僅僅是 印刷它們。
  3. 如果get_prices()是打印價格清單,它會更好 名爲display_prices()或類似的 - get_prize()建議 該方法返回一個值。

那麼你的求和方法可以通過調用get_prices()得到的價格列表,並總結他們:

def summation(self): 
    return sum(self.get_prices()) 
-1

我覺得這可能是一個簡單的解決方案:

class DATA: 
def __init__(self,Price): # skipped other parameters  
    self.Price = Price 

class TOOLBOX(object): 

def __init__ (self): 
    self.alldata = [] 

def add_data(self, Price): 
    self.alldata.append(DATA(Price)) 

def get_prize(self) : 
    price=[] 
    for line in self.alldata: 
      price.append(line.Price) 
    print(price) 
    return price 

def summation(self): 
    return sum(self.get_prize()) 

# use example 
T=TOOLBOX() 
T.add_data(2) 
T.add_data(3) 
print(T.summation()) 
# 5 

get_prize()可以寫得更優雅:

def get_prize(self) : 
    price = [line.Price for line in self.alldata] 
    print(price) 
    return price 
0

更多的提示這個代碼 - (用於回答您的問題,檢查我的其他答案)

Python是一種語言,它允許一個真正寫短,可讀的代碼 - 在這個小例子,你在重複15個屬性的名稱是以上的7倍---這是很多屬性名稱的打字方式:-)

因此,如果您知道您將從csv文件中獲取屬性,並且它不應該改變,您可以將實例代碼更改爲:

for csv_ff中的行: if len(l ine)== 15: dd.add_data(* line)

將「*」前置於行序列之前,會將其每個元素展開爲對.add_data調用的位置參數。 - 這將刪除所有的變量名稱。

你可以做的add_data方法是相同的 - 它可以僅僅指剛:

def add_data(self, *args): 
    self.all_data.append(DATA(*args)) 

的「*」的功能(/法)的定義只是告訴Python來採取所有剩餘的位置參數,並把他們在一個名爲「args」的列表中。 我們不在那裏使用argumetns,我們只是通過它們。按原樣摺疊到對象構造函數的DATA。請注意,這也會將TOOLBOX類從您的對象的特定屬性中分離出來。

還有一些技巧可以縮短DATA的實現方式,但是會有可讀性/複雜性的折衷。 (儘管很小)