Please don't mark as duplicate, other similar questions did not solve my issue.
请不要标记为重复,其他类似的问题没有解决我的问题。
This is my setup
这是我的设置
/main.py
/actions/ListitAction.py
/actions/ViewAction.py
Main.py:
Main.py:
from actions import ListitAction, ViewAction
ListitAction.py:
ListitAction.py:
class ListitAction(object):
def __init__(self):
#some init behavior
def build_uri():
return "test.uri"
ViewAction.py
ViewAction.py
from actions import ListitAction
class ViewAction(ListitAction):
def __init__(self, view_id):
ListitAction.__init__(self)
self.view_id = view_id
def build_uri():
return "test"
Running:
运行:
$ python3 main.py
The only error message I receive is:
我收到的唯一错误信息是:
Traceback (most recent call last):
File "/home/jlevac/workspace/project/listit.py", line 11, in <module>
from actions import ListitAction, ViewAction, CommentsAction
File "/home/jlevac/workspace/project/actions/ViewAction.py", line 3, in <module>
class ViewAction(ListitAction):
TypeError: module.__init__() takes at most 2 arguments (3 given)
Even if I try for the python3 console, I received the same error message:
即使我尝试使用python3控制台,也收到了相同的错误消息:
$python3
from actions import ViewAction
I am new to Python, but not new to programming. I'm assuming that my error messages have to do with the import statements, but based on the message I can't really figure out what it means.
我是Python的新手,但不是编程新手。我假设我的错误消息与导入语句有关,但基于此消息,我无法真正理解它的含义。
Thanks
谢谢
3 个解决方案
#1
14
Your imports are wrong, so you're trying to inherit from the modules themselves, not the classes (of the same name) defined inside them.
您的导入是错误的,所以您尝试从模块本身继承,而不是从它们内部定义的类(同名的类)继承。
from actions import ListitAction
in ViewAction.py
should be:
在ViewAction。py应该是:
from actions.ListitAction import ListitAction
and similarly, all other uses should switch to explicit imports of from actions.XXX import XXX
(thanks to the repetitive names), e.g. from actions import ListitAction, ViewAction
must become two imports:
类似地,所有其他用途都应该转换为从行为的显式导入。XXX导入XXX(由于重复的名称),例如:从操作导入ListitAction, ViewAction必须成为两个导入:
from actions.ListitAction import ListitAction
from actions.ViewAction import ViewAction
because the classes being imported come from different modules under the actions
package.
因为导入的类来自操作包下的不同模块。
#2
0
You are passing self
when you don't need to, that's all.
Edit: see MSeifert's comment answer since I don't want to steal content.
当你不需要的时候,你正在超越自我,仅此而已。编辑:查看MSeifert的评论答案,因为我不想偷内容。
#3
0
Creating classes and instance of variables
class Student:
# Creating a Class Variables
perc_Raise = 1.05
# Creating a constructor or a special method to initialize values
def __init__(self,firstName,lastName,marks):
self.firstName = firstName
self.lastName = lastName
self.email = firstName + "." + lastName +"@northalley.com"
self.marks = marks
def fullName(self):
return '{} {}'.format(self.firstName,self.lastName)
def apply_raise(self):
self.marks = int(self.marks * self.perc_Raise)
Creating a two instance variable for a Student class
std_1 = Student('Mahesh','Gatta',62)
std_2 = Student('Saran','D',63)
print(std_1.fullName())
print(std_1.marks)
std_1.apply_raise()
print(std_1.marks)
print(std_1.email)
print(std_1.__dict__)
print(std_2.fullName())
print(std_2.marks)
std_2.apply_raise()
print(std_2.marks)
print(std_2.email)
print(std_2.__dict__)
print(Student.__dict__)
Inheritance
class Dumb(Student):
perc_Raise = 1.10
def __init__(self,firstName,lastName,marks,prog_lang):
super().__init__(firstName,lastName,marks)
self.prog_lang = prog_lang
std_1 = Dumb('Suresh','B',51,'Python')
print(std_1.fullName())
print(std_1.marks)
std_1.apply_raise()
print(std_1.marks)
print(std_1.prog_lang)
#1
14
Your imports are wrong, so you're trying to inherit from the modules themselves, not the classes (of the same name) defined inside them.
您的导入是错误的,所以您尝试从模块本身继承,而不是从它们内部定义的类(同名的类)继承。
from actions import ListitAction
in ViewAction.py
should be:
在ViewAction。py应该是:
from actions.ListitAction import ListitAction
and similarly, all other uses should switch to explicit imports of from actions.XXX import XXX
(thanks to the repetitive names), e.g. from actions import ListitAction, ViewAction
must become two imports:
类似地,所有其他用途都应该转换为从行为的显式导入。XXX导入XXX(由于重复的名称),例如:从操作导入ListitAction, ViewAction必须成为两个导入:
from actions.ListitAction import ListitAction
from actions.ViewAction import ViewAction
because the classes being imported come from different modules under the actions
package.
因为导入的类来自操作包下的不同模块。
#2
0
You are passing self
when you don't need to, that's all.
Edit: see MSeifert's comment answer since I don't want to steal content.
当你不需要的时候,你正在超越自我,仅此而已。编辑:查看MSeifert的评论答案,因为我不想偷内容。
#3
0
Creating classes and instance of variables
class Student:
# Creating a Class Variables
perc_Raise = 1.05
# Creating a constructor or a special method to initialize values
def __init__(self,firstName,lastName,marks):
self.firstName = firstName
self.lastName = lastName
self.email = firstName + "." + lastName +"@northalley.com"
self.marks = marks
def fullName(self):
return '{} {}'.format(self.firstName,self.lastName)
def apply_raise(self):
self.marks = int(self.marks * self.perc_Raise)
Creating a two instance variable for a Student class
std_1 = Student('Mahesh','Gatta',62)
std_2 = Student('Saran','D',63)
print(std_1.fullName())
print(std_1.marks)
std_1.apply_raise()
print(std_1.marks)
print(std_1.email)
print(std_1.__dict__)
print(std_2.fullName())
print(std_2.marks)
std_2.apply_raise()
print(std_2.marks)
print(std_2.email)
print(std_2.__dict__)
print(Student.__dict__)
Inheritance
class Dumb(Student):
perc_Raise = 1.10
def __init__(self,firstName,lastName,marks,prog_lang):
super().__init__(firstName,lastName,marks)
self.prog_lang = prog_lang
std_1 = Dumb('Suresh','B',51,'Python')
print(std_1.fullName())
print(std_1.marks)
std_1.apply_raise()
print(std_1.marks)
print(std_1.prog_lang)