2013-09-27 47 views
1

其实,我想写如何运用自己的方法参数在Python

def w_cell(self, r, c, val, style=XLS.get_cell_stlye(self)) 

但它不能,并让我

NameError错误:名称“自我”是没有定义

如何在参数中设置默认样式,而不是在函数中。

感谢

class XLS(): 

    def get_cell_stlye(self):   
     return xlwt.easyxf("alignment: horizontal center,\ 
     vertical center, wrap true ;") 


class TestCGI(object, XLS): 
    def w_cell(self, r, c, val, style='null'): 
      if style == 'null': 
       style=XLS.get_cell_stlye(self) 
      self.sht.write(r, c, val, style) 

回答

0

在函数本身微调吧,虽然你可能想要做这样的:

class TestCGI(object, XLS): 
    def w_cell(self, r, c, val, style=None): 
     if style is not None: 
      style=XLS.get_cell_stlye(self) 
     self.sht.write(r, c, val, style) 
+0

在这种情况下,我会是'如果风格不None' - 你的版本也将拿起'style = 0',这可能有意义,也可能没有意义。 – mgilson

0

简短的回答是,你不能写。在创建函数时,对默认参数进行评估。在函数创建时,self尚不存在,所以它不能与计算默认值有关。

1毕竟,如果还不存在,也不能有任何实例 - 即使它做了,哪个实例将它拆了是self

相关问题