当你从一个模块而不是一个Python类中固有的时候会发生什么? [英] What happens when you inherent from a module instead of a class in Python?

查看:252
本文介绍了当你从一个模块而不是一个Python类中固有的时候会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了此问题


  import Object 

class Visitor(Object):

def __init __(self):
super(Visitor,self).__ init __()
def visit(self,obj):
pass
def getIsDone (self):
return False
isDone = property(fget = lambda self:self.getIsDone())

$ b b

我得到这个错误:

  TypeError:module .__ init __ 3 given)


及其答案:


  class A:pass 
print(A)#outputs< class'__main __。A'>
import urllib
print(urllib)#outputs< module'urllib'from'/usr/lib/python3.2/urllib/__init__.py'>

您的错误发生,因为Object是一个模块,而不是一个类。



将您的导入语句更改为:

 

code>从对象导入ClassName

,类定义为:

  class Visitor(ClassName):



将您的课程定义更改为:

  Visitor(Object.ClassName):
etc




'不是真的满意这个答案,因为我不是真的确实如何从该错误消息得出的结论,我不小心继承了一个模块而不是一个类。我想知道,如果有人可以阐述为什么这个错误发生,什么是给定的参数是什么?当python解释器遇到这样的代码:
class Employee(Person)发生了什么?回答者是什么意思是我的遗产是什么?感谢您对资源的任何解释或参考。

解决方案

如果您放置一个 BaseClass 在继承列表中,然后解释器将在内部调用这个方法:

  type(BaseClass).__ init __(cls, name_of_subclass,(BaseClass,),dict_of_subclass)
#或更简单
类型(BaseClass)(name_of_subclass,(BaseClass,),dict_of_subclass)

您可以创建一个虚拟的BaseClass来测试它

  class Meta ):
def __init __(self,name,base,subcls):
print(self,name,base,subcls)

Base = Meta('','' '')

class Test(Base):
prop1 =hello

其输出:

 < __ main __。在0x7f7471666bd0的元对象> 
< __ main __。在0x7f7471666c50的元对象>测试(< __ main __。在0x7f7471666bd0>上的元对象),{'__module__':'__main__','prop1':'hello'}

要回答你的问题:当解释器看到 class Employee(Person):pass 时,会发生以下情况:

  type(Person).__ init __(cls,'Employee',(Person,),{'__module__':'__main__'})

如果 Person 是一个普通类, type(person)将返回类型本身。

如果 Person 是一个模块, type(person)将返回对象模块,其中有一个方法 __ init __ 。但是这个方法只有2个参数,你会得到一个错误。

  import sys 
type(sys).__ init__ (sys,2,3,4)
#Traceback(最近最后调用):
#文件testClassInheritance.py,第11行,在< module>
#type(sys).__ init __(sys,2,3,4)
#TypeError:module .__ init __()最多取2个参数(3给出)


I recently came across this question.

import Object

class Visitor(Object):

    def __init__(self):
        super(Visitor,self).__init__()
    def visit(self, obj):
        pass
    def getIsDone(self):
        return False
    isDone = property(fget =lambda self:self.getIsDone())

I get this error:

TypeError: module.__init__() takes at most 2 arguments (3 given)

and its answer:

class A:pass
print(A)              #outputs <class '__main__.A'>
import urllib
print(urllib)         #outputs <module 'urllib' from '/usr/lib/python3.2/urllib/__init__.py'>

Your error is happening because Object is a module, not a class. So your inheritance is screwy.

Change your import statement to:

from Object import ClassName

and your class definition to:

class Visitor(ClassName):

or

change your class definition to:

class Visitor(Object.ClassName):
   etc

I'm not really satisfied with this answer as I'm not really sure how I get from that error message to the conclusion that I am accidentally inheriting from a module instead of a class. I was wondering if somebody could elaborate on why this error is occurring and what exactly the arguments being given are? When the python interpreter comes across code like this: class Employee(Person) what is happening? What exactly does the answerer mean by my inheritance is screwy? Thanks for any explanations or references to resources.

解决方案

If you put an object called BaseClass in the inheritance list, then the interpreter will call this internally:

type(BaseClass).__init__(cls, name_of_subclass, (BaseClass,), dict_of_subclass)
# or simpler
type(BaseClass)(name_of_subclass, (BaseClass,), dict_of_subclass)

You can create a dummy BaseClass to test it

class Meta(object):
   def __init__(self, name,   base,  subcls):
      print (self, name,   base,  subcls)

Base = Meta('','','')

class Test(Base):
    prop1="hello"

which outputs:

<__main__.Meta object at 0x7f7471666bd0>
<__main__.Meta object at 0x7f7471666c50> Test (<__main__.Meta object at 0x7f7471666bd0>,) {'__module__': '__main__', 'prop1': 'hello'}

To answer your question: When the interpreter sees class Employee(Person): pass, the following will happen:

type(Person).__init__(cls, 'Employee', (Person,), {'__module__': '__main__'})

If Person is a normal class, type(person) will return type itself. Then type.__init__ will get called.

If Person is a module, type(person) will return the object module, which has a method __init__. But this method only takes 2 argument, there you get an error.

import sys
type(sys).__init__(sys,2,3,4)
#Traceback (most recent call last):
#  File "testClassInheritance.py", line 11, in <module>
#    type(sys).__init__(sys,2,3,4)
#TypeError: module.__init__() takes at most 2 arguments (3 given)

这篇关于当你从一个模块而不是一个Python类中固有的时候会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆