2010-09-20 66 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期待一个字符串,而不是整数,发生错误。

相关问题