简体   繁体   English

Python-为什么2个不同的函数将相同的元组标识为 <class 'tuple'> 而另一个函数将相同的元组标识为None

[英]Python - Why 2 different functions identify the same tuple as <class 'tuple'> while another function identifies the same tuple as None

So when I call 所以当我打电话

plot_bounces( ball, numbounces, boxWH_tuple, circle=None )

where ball is an object, numbounces is an int, boxWH_tuple and circle are tuples, and put a print(type(circle)) statement in this function, it is a tuple. 其中ball是一个对象,numbounces是一个int,boxWH_tuple和circle是元组,并在该函数中放置一个print(type(circle))语句,它是一个元组。 However, when I call update_ball(ball, boxWH_tuple, circle=None) an put a print(type(circle)) statement in this function, it is a nonetype. 但是,当我在此函数中调用update_ball(ball, boxWH_tuple, circle=None)并放置一条print(type(circle))语句时,它是nonetype。 I don't understand why nor how to make it a tuple for both functions. 我不明白为什么也不能将它变成两个函数的元组。 This is just part of my code which seems to be problematic. 这只是我的代码的一部分,似乎有问题。

This is what I used to call plot_bounces 这就是我以前所说的plot_bounces

plot_bounces(Ball(-1,0,0.1), 1, (3,2), ( (0,0) ,0.4) )

There's another script with class Ball which deals with the Ball object. 还有另一个带有Ball类的脚本,该脚本处理Ball对象。

My main concern is why print(type(circle)) gives me class 'tuple' for the first print and class 'NoneType' for the second print. 我主要关心的是为什么print(type(circle))给我第一个打印类“ tuple”和第二个打印类“ NoneType”。

def plot_bounces(ball, numbounces, boxWH_tuple, circle=None):
    print(type (circle))
     #make space to store coordinates 
    xcoords = []
    ycoords = []
    #store the starting point 
    xcoords.append(ball.x)
    ycoords.append(ball.y)


    while numbounces>0 :
        #calls on update_ball and change the old values with new ones 

        update_ball(ball, boxWH_tuple, circle=None)
#       
        ball=update_ball(ball, boxWH_tuple, circle=None)

        xpoint = ball.x
        ypoint = ball.y

        ycoords.append(ypoint)
        xcoords.append(xpoint)

        numbounces+= -1


def update_ball(ball, boxWH_tuple, circle=None):
    print (type(circle))

Should't it be a tuple for both cases? 这两种情况都不应该是元组吗?

When calling update_ball() from plot_bounces() , please remove =None after circle, because when you do circle=None in a call (call, not function definition), then you are passing that argument as a None and therefore the confusion. 当从plot_bounces()调用update_ball() ,请在循环后删除=None ,因为在调用(调用而不是函数定义)中进行circle=None时,您会将该参数作为None传递,因此造成混淆。

The call should be update_ball(ball, boxWH_tuple, circle) 该呼叫应为update_ball(ball, boxWH_tuple, circle)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM