2010-09-20 24 views
2

我知道這可能是一個愚蠢的問題,但我是Python中的OOP的新手,如果我聲明函數def myFunction(b)並將對象的實例傳遞給它,我會得到TypeError:預期的字符串或緩衝區。如何聲明一個將實例作爲Python參數的方法?

爲了更具體一些,我使用下面的代碼來解析總結分子式並將其作爲對象。

class SummaryFormula: 
    def __init__(self, summaryFormula): 
     self.atoms = {} 
     for atom in re.finditer("([A-Z][a-z]{0,2})(\d*)", summaryFormula): 
      symbol = atom.group(1) 
      count = atom.group(2) 

    def extend(self, b): 
     # these are the two dictionaries of both molecules 
     originalFormula = self.atoms.copy() 
     self.atoms.clear() 
     addAtoms = SummaryFormula(b) 

     # and here both dictionaries are merged 
     for atom in addAtoms.atoms.keys(): 
      if atom in originalFormula.keys(): 
      self.atoms[ atom] = originalFormula[ atom] 
      self.atoms[ atom] += addAtoms.atoms[ atom] 
      else: 
      pass 
     for atom in originalFormula.keys(): 
      if atom not in self.atoms.keys(): 
      self.atoms[ atom] = originalFormula[ atom] 

#this is what works now 
test = SummaryFormula("H2CFe2") 
test.extend("H5C5") #result is a molecule H7C6Fe2 

#this is what I want instead 
test = SummaryFormula("H2CFe2") 
toExtend = SummaryFormula("H5C5") 
test.extend(toExtend) 

謝謝,托馬斯

+0

你能提供一個代碼示例嗎? – 2010-09-20 22:25:20

+0

我認爲,爲了幫助您,我們需要更多的背景信息。 「myFunction」的實際定義是什麼?這是一個普通的功能嗎?一個方法?什麼是堆棧跟蹤,除了例外情況? – Dirk 2010-09-20 22:25:28

+0

小心向我們展示函數定義?使用提供的信息我無法說出任何內容。 – dekomote 2010-09-20 22:25:31

回答

1

理查德庫克是正確的。還有一個問題,但是:在extend,你說:

addAtoms = SummaryFormula(b) 

因此,SummaryFormula實例傳遞到SummaryFormula的__init__方法。這裏(模之前提到的錯字),該目的是給予re.finditer

for atom in re.finditer("([A-Z][a-z]{0,2})(\d*)", summaryFormula) 

功能re.finditer期望的字符串;它不知道如何處理SummaryFormula實例。

有幾種方法可以解決這個問題。緊接簡單的方法是檢查你是否已經有了一個SummaryFormula實例試圖創建一個前:

if isinstance(b, SummaryFormula): 
    addAtoms = b 
else if isinstance(b, str): 
    addAtoms = SummaryFormula(b) 
else: 
    raise TypeError("Expected a SummaryFormula or equivalent string.") 
+0

完美,非常感謝! – 2010-09-20 23:30:05

+0

不客氣。祝你好運。 – 2010-09-21 23:29:41

1

首先,該方案需要包括re模塊。

其次,你在第4行有一個錯字:

for atom in re.finditer("([A-Z][a-z]{0,2})(\d*)", SummaryFormula): 

summaryFormula

for atom in re.finditer("([A-Z][a-z]{0,2})(\d*)", summaryFormula): 

即小寫s

SummaryFormula指的是類的名稱,而summaryFormula指的是__init__方法的第二個參數(在self之後)。

三,線addAtoms = SummaryFormula(b)正在經過的SummaryFormula實例作爲自變量b(分配在腳本test.extend(toExtend)的頂層部分

的固定程序應該看起來像:

import re 

class SummaryFormula: 
    def __init__(self, summaryFormula): 
     self.atoms = {} 
     for atom in re.finditer("([A-Z][a-z]{0,2})(\d*)", summaryFormula): 
      symbol = atom.group(1) 
      count = atom.group(2) 

    def extend(self, b): 
     # these are the two dictionaries of both molecules 
     originalFormula = self.atoms.copy() 
     self.atoms.clear() 

     # PASS AN APPROPRIATE VALUE HERE! 
     addAtoms = SummaryFormula("SOME STRING") 

     # and here both dictionaries are merged 
     for atom in addAtoms.atoms.keys(): 
      if atom in originalFormula.keys(): 
      self.atoms[ atom] = originalFormula[ atom] 
      self.atoms[ atom] += addAtoms.atoms[ atom] 
      else: 
      pass 
     for atom in originalFormula.keys(): 
       if atom not in self.atoms.keys(): 
      self.atoms[ atom] = originalFormula[ atom] 

#this is what works now 
test = SummaryFormula("H2CFe2") 
test.extend("H5C5") #result is a molecule H7C6Fe2 

#this is what I want instead 
test = SummaryFormula("H2CFe2") 
toExtend = SummaryFormula("H5C5") 
test.extend(toExtend) 

"SOME STRING"替換通過預期的字符串字符串或字符串變量引用我不知道該程序的確切意圖,所以我會留給其他人來確定此程序應該傳遞給此構造函數SummaryFormula

希望有幫助!

+0

謝謝,我修復了這個問題,但仍不能解決問題。 – 2010-09-20 23:16:23

+0

有關「addAtoms = SummaryFormula(b)」行的註釋的擴展答案。 – 2010-09-20 23:19:44

1

總之你可以傳遞任何對象的功能,其中包括創建類的實例。

更一般地說,Python中的所有東西都是一個對象。這是一個關鍵概念在Python中,所以如果你是新的語言花費一些時間熟悉,因爲它會幫助你的代碼更簡潔和pythonic。

你得到的錯誤不是傳遞一個類實例對象,而是在另一個函數或操作期望的字符串中產生某處(請參閱其他答案,因爲它們看起來在它上面)或類似字符串的對象來操作。例如,你可以通過執行產生類似的錯誤:

>>> a = 2 
>>> open(a, 'r') 

TypeError: coercing to Unicode: need string or buffer, int found 

這裏,因爲文件打開功能open期待一個字符串,而不是整數,發生錯誤。

相關問題