python参数后面的中括号_Python中作为参数的方法是否带小括号的区别

瞄准细节,方得始终

今觅得一码,见其——”作为参数的方法没有带括号也能运行”,诧异,整理若下

一、先演示没有括号的

设有方法

def move_turtle(self):

self.turtle.forward(50)

执行调用

self.move_btu.clicked.connect(self.move_turtle)

没有括号的时候,是告诉Python运行改方法时将 该方法本身作为参数,而不是其计算结果

二、带括号的

有括号的时候的,则返回括号内方法的计算结果。

distance_spin = QSpinBox()

distance_spin.setValue(20)

turtle.forward(distance_spin.value())

这里最后一句的调用就加了 小括号 ,所以最后应该是返回20,

截断代码不好理解放上完整代码

import turtle

import sys

from PySide2.QtCore import *

from PySide2.QtGui import *

from PySide2.QtWidgets import *

class TuControl(QWidget):

def __init__(self,turtle):

super(TuControl,self).__init__()

self.turtle = turtle

self.left_btu = QPushButton("Left",self)

self.right_btu = QPushButton("Right",self)

self.move_btu = QPushButton("Move",self)

self.distance_spin = QSpinBox()

self.controlsLayout = QGridLayout()

self.controlsLayout.addWidget(self.left_btu,0,0)

self.controlsLayout.addWidget(self.right_btu,0,1)

self.controlsLayout.addWidget(self.distance_spin ,1,0)

self.controlsLayout.addWidget(self.move_btu,1,1)

self.setLayout(self.controlsLayout)

self.distance_spin.setRange(0,100)

self.distance_spin.setSingleStep(5)

self.distance_spin.setValue(20)

self.move_btu.clicked.connect(self.move_turtle)

self.right_btu.clicked.connect(self.turn_turtle_right)

self.left_btu.clicked.connect(self.turn_turtle_left)

def turn_turtle_left(self):

self.turtle.left(45)

def turn_turtle_right(self):

self.turtle.right(45)

def move_turtle(self):

self.turtle.forward(self.distance_spin.value())

#set up

window = turtle.Screen()

bab = turtle.Turtle()

#Create a Qt application

app = QApplication(sys.argv)

control_window = TuControl(bab)

control_window.show()

#main loop

app.exec_()

sys.exit()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值