在同一个模块中,为什么有的类可以用,有的就不行了呢?

软件和网站开发以及相关技术探讨
回复
悬崖勒马2011
帖子: 91
注册时间: 2013-01-23 14:27
系统: ubuntu 12.04

在同一个模块中,为什么有的类可以用,有的就不行了呢?

#1

帖子 悬崖勒马2011 » 2013-05-28 15:01

到第四个类就无法引用了,而且第三个类的self居然也没法用了。谢谢!

代码: 全选

class Person:
 def setName(self,name):
  self.name = name

 def getName(self):
  return self.name

 def greet(self):
  print "Hello.World!I'm %s." % self.name

class Secretive:
  def __inaccessible(self):
    print "But you can't see me.."
  def accessible(self):
    print "the secret message is:"
    self.__inaccessible()

class numbercounter:
  number=0
  def init(self):
   numbercounter.number +=1

class filter:
  def init(self):
      self.blocked=[]
  def filter(self,sequence):
      return [x for x in sequence if x not in self.blocked]
悬崖勒马2011
帖子: 91
注册时间: 2013-01-23 14:27
系统: ubuntu 12.04

Re: 在同一个模块中,为什么有的类可以用,有的就不行了呢?

#2

帖子 悬崖勒马2011 » 2013-05-28 15:27

self 不能用的原因:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method init() must be called with abcfilter instance as first argument (got nothing instead)
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: 在同一个模块中,为什么有的类可以用,有的就不行了呢?

#3

帖子 枫叶饭团 » 2013-05-28 15:28

因为你给我看的代码都没有缩进
悬崖勒马2011
帖子: 91
注册时间: 2013-01-23 14:27
系统: ubuntu 12.04

Re: 在同一个模块中,为什么有的类可以用,有的就不行了呢?

#4

帖子 悬崖勒马2011 » 2013-05-29 13:39

枫叶饭团 写了:因为你给我看的代码都没有缩进
缩进我有,只是复制到上面的都没了。
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

Re: 在同一个模块中,为什么有的类可以用,有的就不行了呢?

#5

帖子 oneleaf » 2013-05-29 14:01

悬崖勒马2011 写了:
枫叶饭团 写了:因为你给我看的代码都没有缩进
缩进我有,只是复制到上面的都没了。
用Code包一下代码就好了。
悬崖勒马2011
帖子: 91
注册时间: 2013-01-23 14:27
系统: ubuntu 12.04

Re: 在同一个模块中,为什么有的类可以用,有的就不行了呢?

#6

帖子 悬崖勒马2011 » 2013-05-29 14:08

现在没事了,今天重开机后这些代码居然都能运行了。问题解决!谢谢各位
回复