1. 错误提示

2. 代码

class Parent:
"""定义父类"""
def __init__(self):
print("调用父类构造函数")

  

import Parent

class Child(Parent):
"""定义子类"""
def __init__(self):
print("调用子类构造方法") child = Child() # 实例化子类

  

3. 错误原因

此处想要导入类,如上代码所示只是导入了模块,Python的模块名与类名是在两个不同的名字空间中,初学者很容易将其弄混淆。

python 类

用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例

python 模块

模块,在Python可理解为对应于一个文件。

根据上面代码,你想使用 import Parent 导入Parent 类,但 import Parent 只能导入模块,所以错误

4. 解决方法

方法一
使用正确方式导入类, import Parent from Parent (此操作就是导入Parent 模块中的 Parent 类)

方法二
修改 class Child(Parent): 代码为 class Child(Parent.Parent):,目的也是选中模块中的类

5. 正确调用的代码

  方法一

from Parent import Parent

class Child(Parent):
"""定义子类"""
def __init__(self):
print("调用子类构造方法") child = Child() # 实例化子类

  

方法二

  

import Parent

class Child(Parent.Parent):
"""定义子类"""
def __init__(self):
print("调用子类构造方法") child = Child() # 实例化子类

  

转载

Python:彻底理解并解决错误TypeError: module.__init__() takes at most 2 arguments (3 given)_不懂一休-CSDN博客

TypeError: module() takes at most 2 arguments (3 given)的更多相关文章

  1. python3在使用类基础时,遇到错误TypeError: module.**init**() takes at most 2 arguments (3 given)

    python3在使用类基础时,遇到错误TypeError: module.init() takes at most 2 arguments (3 given) 1.原因:直接导入的py文件,而没有导入 ...

  2. Python基础-TypeError:takes 2 positional arguments but 3 were given

    Error: 今天写一段简单类定义python代码所遇到报错问题:TypeError: drive() takes 2 positional arguments but 3 were given 代码 ...

  3. python:TypeError: main() takes 0 positional arguments but 1 was given

    TypeError: main() takes 0 positional arguments but 1 was given def main(self): 括号里加上self就好了

  4. Python中错误之 TypeError: object() takes no parameters、TypeError: this constructor takes no arguments

    TypeError: object() takes no parameters TypeError: this constructor takes no arguments 如下是学习python类时 ...

  5. TypeError: Restaurant() takes no arguments

    1. 错误描述 TypeError: Restaurant() takes no arguments 2. 原因:在编写__init__时,pycharm会自动添加关键字,有时会直接写称整型int, ...

  6. TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133

    程序代码  class Person:      #constructor      def __init__(self,name,sex):           self.Name = name   ...

  7. python3.5安装pyHook,解决【TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'time', 'hwnd', and 'window_name'】这个错误!

    为什么安装 pyHook包:为Windows中的全局鼠标和键盘事件提供回调. Python应用程序为用户输入事件注册事件处理程序,例如鼠标左键,鼠标左键,键盘键等 先要实时获取系统的鼠标位置或者键盘输 ...

  8. tensorflow 升级到1.9-rc0,tensorboard 报错:TypeError: GetNext() takes exactly 1 argument (2 given)

    Exception in thread Reloader:Traceback (most recent call last):  File "/usr/lib/python2.7/threa ...

  9. Python TypeError: 'module' object is not callable 原因分析

    今天尝试使用pprint进行输出,语句为 >>>import pprint >>>pprint(people) 结果报错,TypeError: 'module' o ...

随机推荐

  1. MySQL——优化

    MySQL数据库优化: 1.优化角度 安全: 数据可持续性 性能: 数据的高性能访问 2.优化范围(优化顺序---->) (1)存储.主机和操作系统: 主机架构稳定性 I/O规划及配置 swap ...

  2. JS_DOM操作之绑定事件

    1 - 静态绑定:直接把事件写在标签元素中 <div id="div" onclick="foo(this)">click</div> ...

  3. python tif转jpg

    在同级目录完成tif和jpg的批量转换 import os import cv2 import numpy as np from osgeo import gdal #数据格式转化 def norma ...

  4. NOIP模拟测试17&18

    NOIP模拟测试17&18 17-T1 给定一个序列,选取其中一个闭区间,使得其中每个元素可以在重新排列后成为一个等比数列的子序列,问区间最长是? 特判比值为1的情况,预处理比值2~1000的 ...

  5. JS007. 深入探讨带浮点数运算丢失精度问题(二进制的浮点数存储方式)

    复现与概述 当JS在进行浮点数运算时可能产生丢失精度的情况: 从肉眼可见的程度上观察,发生精度丢失的浮点数是没有规律的,但该浮点数丢失精度的问题会100%复现.经查阅,这个问题要追溯至浮点数的二进制存 ...

  6. throw关键字

    1.基础用法 2.方法中加合法校验,告知方法的调用者 数组越界判断 3.一切皆为对象,创建的是运行期对象,则可以不处理(throws/try catch),直接交给JVM处理(打印并终止程序) 4.O ...

  7. Input 只能输入数字,数字和字母等的正则表达式

    JS只能输入数字,数字和字母等的正则表达式 1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace( ...

  8. 计算机网络-TCP篇

    TCP篇 之前的总结文章:TCP简单版本介绍-三次握手等 基本认识 TCP 是⾯向连接的(⼀定是「⼀对⼀」才能连接).可靠的.基于字节流的传输层通信协议. RFC 793 是如何定义「连接」的:⽤于保 ...

  9. python多继承简单方法

    class people(object): #建创一个人类 def __init__(self,name,age): self.name = name self.age = age def eat(s ...

  10. 解决wampserver无法启动问题

    如果无法启动,找不到原因.直接依次点击打开到:控制面板--管理工具--事件查看器--windows日志--应用程序,查看对应进程错误信息对症下药即可. 我这个错误就是8099端口错误,运行cmd命令, ...