为了账号安全,请及时绑定邮箱和手机立即绑定

类型错误位置参数自我丢失

类型错误位置参数自我丢失

饮歌长啸 2022-05-24 09:23:00
为什么我无法访问 Classroom 对象?它显示: next_scene_name = start_scene.enter() #error TypeError: enter() missing 1 required positional argument: 'self'请解释我犯了什么错误。#classclass Engine(object):    #(map_object_with_classroom, player_object)    def __init__(self,map_object,player_object):        self.map_object = map_object        self.player_object = player_object    def play(self):        print('\n')        print('test ok')        start_scene = self.map_object.begin()        end_scene = self.map_object.Senario['auditorium']        while start_scene != end_scene:            next_scene_name = start_scene.enter() #error            start_scene = self.map_object.next_scene(next_scene_name)class Player(object):    Student_influence = 20    Staff_influence = 20    principle_influence = 20    def __init__(self,name):        self.name = nameclass Scene(object):    def enter(self):        print("test")class ClassRoom(Scene):    def enter(self):        print('test ok!')        return 'sportsclub'class SportsClub(Scene):    def enter(self):        print('test ok!')        return 'scienceclub'class ScienceClub(Scene):    def enter(self):        print('test ok!')        return 'canteen'class Canteen(Scene):    def enter(self):        print('test ok!')        return 'auditorium'class Auditorium(Scene):    def enter(self):        print('test ok!')        return 'finished'class Finished(Scene):    def enter(self):        print('test ok!')        return Noneclass Map(object):    Senario = {'finished':Finished, 'classroom':ClassRoom,'sportclub':SportsClub, 'scienceclub':ScienceClub, 'canteen':Canteen, 'auditorium':Auditorium}    def __init__(self,start_scene):        self.start_scene = start_scene    def begin(self):        return self.next_scene(self.start_scene)    def next_scene(self,scene_name):        nxt = self.Senario.get(scene_name)        return nxt当我打印 start_scene 它显示那是什么?但是当我打印 ClassRoom 类的实例时,它显示 < main .ClassRoom object>
查看完整描述

1 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

在next_scene中,您正在返回,但ClassRoom不是. 为此,您需要,然后有一个ClassRoomreturn nxt()self


class Map(object):

    Senario = {'finished':Finished, 'classroom':ClassRoom,'sportclub':SportsClub, 'scienceclub':ScienceClub, 'canteen':Canteen, 'auditorium':Auditorium}


    def __init__(self,start_scene):

        self.start_scene = start_scene


    def begin(self):

        return self.next_scene(self.start_scene)


    def next_scene(self,scene_name):

        # adding the parentheses here will call the class

        # creating an instance

        nxt = self.Senario.get(scene_name)()

        return nxt

举个小例子:


class X:

    def something(self):

        return 4



X.something()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: something() missing 1 required positional argument: 'self'

您还没有创建任何实例,因此self还不存在。但是,如果我们将其更改为:


x = X()

x.something()

4

Aself被创建是因为底层的类机制将调用 的基本实现__init__,而不涉及血淋淋的细节


查看完整回答
反对 回复 2022-05-24
  • 1 回答
  • 0 关注
  • 93 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信