当一个类中的函数返回一个值,它在哪里返回? [英] When a function in a class returns a value, where is it returned to?

查看:238
本文介绍了当一个类中的函数返回一个值,它在哪里返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习Classes,我得到了我的代码,但我只是想知道返回的值在哪里?



code:

 从sys导入exit 
从随机导入randint

class Game(object):

def __init __(self,start):
self.pie = ['pie test']

self.start = start

def play(self):
next_room_name = self.start
while True:
print\\\
--------
room = getattr(self,next_room_name )
next_room_name = room()

def rooom(self):
print测试工作良好
返回piez

def piez(self):
printpie room
exit(1)


a_game =游戏(rooom)
a_game .play()

例如,在上面的代码中,它现在需要我到piez(self)功能。但是,返回值是什么地方带我去那里?



在类外返回piez,并用新值重新输入类吗?它返回字符串piez到 init (self,start)?我只是将值返回到上一个函数,play(self)?



除此之外,一切都运行正常,但我只是不明白我是如何到达piez(self)函数,只是返回piez以前的功能。在过去,我跳过函数只是通过键入它的名称(),但这种方式我返回一个字符串,不知道我怎么跳。



这里是我的观察:



1。)实例化类Game(Rooom)到a_game



2。)从a_game我们调用play()函数



3。)Rooom分配给self.start



4。)play(self)开始并使用初始化函数self.start并设置为next_room_name



5。)循环启动。下一个函数从next_room_name获取并分配给房间



6。)next_room_name分配给room()。突然跳到rooom(self)



7。)我们到达rooom(self)并返回piez



8)。 Piez回到某个地方......我们现在在piez(self)



有人可以帮我填补空白?这让我疯狂。我希望我有意义...我刚刚一直在努力理解这一个小时,现在,即使打破了它,我似乎是误解的东西。

解决方案

这里有些混乱:


5。)循环启动。下一个函数从next_room_name获取并分配给房间



6。)next_room_name分配给room()。突然跳到rooom(self)


任何语句的返回值都分配给等号的左边,如果



x = hello()

code>



这意味着:



调用hello并将任何返回值存储在



因此应该是



code> room()
存储在 next_room_name



为你澄清它。


I've been learning about Classes and I got my code to work, but I'm just wondering where does the returned value go?

code:

from sys import exit
from random import randint

class Game(object):

    def __init__(self, start):
        self.pie = [ 'pie test']

        self.start = start

    def play(self):
        next_room_name = self.start
        while True:
            print "\n--------"
            room = getattr(self, next_room_name)
            next_room_name = room()

    def rooom(self):
        print "Test worked. good job"
        return "piez"

    def piez(self):
        print "pie room"
        exit(1)


a_game = Game("rooom")
a_game.play()

For example, in the code above I return "piez" in rooom() and it now takes me to the piez(self) function. However, where did the return value go to take me there?

Did it return "piez" outside the class and re-enter the class with the new value? Did it return the string "piez" to init(self, start) ? Did I just return the value to the previous function, play(self)?

Other than that, everything is working fine, but I just don't understand how I arrived at the piez(self) function, simply by returning "piez" in the previous function. In the past I jumped around functions just by typing in the name of it with (), but this way I'm returning a string and not sure how I'm jumping around.

Here's my observation:

1.) Instantiate class Game("Rooom") to a_game

2.) from a_game we call the play() function

3.) "Rooom" is sent to init and assigned to self.start

4.) play(self) starts and uses intialized function self.start and sets to next_room_name

5.) Loop starts. Next function is grabbed from next_room_name and assigned to room

6.) next_room_name is assigned to room() . Suddenly jump to rooom(self)

7.) We arrived at rooom(self) and return "piez"

8.) ?? Piez is returned somewhere... and we are now at piez(self)

Can someone help me fill in the gaps? It's driving me crazy. I hope I'm making sense... I've just been closely trying to understand this for an hour now, and even after breaking it down I seem to be misunderstanding something.

解决方案

You have slight confusion here:

5.) Loop starts. Next function is grabbed from next_room_name and assigned to room

6.) next_room_name is assigned to room() . Suddenly jump to rooom(self)

The return value of any statement is assigned to the left hand side of the equal sign, if the equal sign exists.

When you write

x = hello()

This means:

"Call hello and store any return value in x"

So it should be

"The return value of the function called by room() is stored in next_room_name"

Hopefully this clarifies it for you.

这篇关于当一个类中的函数返回一个值,它在哪里返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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