本文将带您了解关于Python:在函数之间传递变量的新内容,同时我们还将为您解释python函数之间传递参数的相关知识,另外,我们还将为您提供关于c–通过指针在函数之间传递向量、iphone–在视图之
本文将带您了解关于Python:在函数之间传递变量的新内容,同时我们还将为您解释python函数之间传递参数的相关知识,另外,我们还将为您提供关于c – 通过指针在函数之间传递向量、iphone – 在视图之间传递变量的最佳方法、Javascript:在函数之间传递大对象或字符串被认为是一种不好的做法、jenkins-在工作之间传递变量?的实用信息。
本文目录一览:- Python:在函数之间传递变量(python函数之间传递参数)
- c – 通过指针在函数之间传递向量
- iphone – 在视图之间传递变量的最佳方法
- Javascript:在函数之间传递大对象或字符串被认为是一种不好的做法
- jenkins-在工作之间传递变量?
Python:在函数之间传递变量(python函数之间传递参数)
我花了过去的几个小时在这里和其他地方进行阅读和实验,但是我并没有真正理解我确信这是一个非常基本的概念:在不同的函数之间传递值(作为变量)。
例如,我将一堆值分配给一个函数中的列表,然后想稍后在另一个函数中使用该列表:
list = []def defineAList(): list = [''1'',''2'',''3''] print "For checking purposes: in defineAList, list is",list return listdef useTheList(list): print "For checking purposes: in useTheList, list is",listdef main(): defineAList() useTheList(list)main()
基于对函数参数的作用的理解,我希望这样做如下:
- 将“列表”初始化为空列表;打电话给main(至少,我知道我没事…)
- 在defineAList()中,将某些值分配给列表;然后将新列表传递回main()
- 在main()中,调用useTheList(list)
- 由于“ list”包含在useTheList函数的参数中,因此我希望useTheList现在将使用defineAList()定义的列表,而不是调用main之前定义的空列表。
但是,这显然是错误的理解。我的输出是:
For checking purposes: in defineAList, list is [''1'', ''2'', ''3'']For checking purposes: in useTheList, list is []
那么,既然“返回”显然没有按照我的想法去做,或者至少没有按照我认为的那样去做……实际上它在做什么?您能否使用本示例向我展示如何将列表从defineAList()中取出并在useTheList()中使用?当我看到事情发生时,我往往会更好地理解它们,但是我看到的许多正确传递参数的示例也都使用了我不熟悉的代码,并且在弄清楚发生了什么事情的过程中,我我并没有真正了解这个概念。我正在使用2.7。
过去,ETA提出了类似的问题,有人建议我使用全局变量而不是局部变量。如果在这里也很重要-就我所上课的目的而言,我们不允许使用全局变量。
谢谢!
答案1
小编典典这是实际发生的情况:
global_list = []def defineAList(): local_list = [''1'',''2'',''3''] print "For checking purposes: in defineAList, list is", local_list return local_listdef useTheList(passed_list): print "For checking purposes: in useTheList, list is", passed_listdef main(): # returned list is ignored returned_list = defineAList() # passed_list inside useTheList is set to global_list useTheList(global_list)main()
这就是你想要的:
def defineAList(): local_list = [''1'',''2'',''3''] print "For checking purposes: in defineAList, list is", local_list return local_listdef useTheList(passed_list): print "For checking purposes: in useTheList, list is", passed_listdef main(): # returned list is ignored returned_list = defineAList() # passed_list inside useTheList is set to what is returned from defineAList useTheList(returned_list)main()
您甚至可以跳过临时returned_list
变量,并将返回值直接传递给useTheList
:
def main(): # passed_list inside useTheList is set to what is returned from defineAList useTheList(defineAList())
c – 通过指针在函数之间传递向量
主要课程:
std::vector<float> * dvertex=NULL; track.calculate(irrelevant stuff,dvertex)
二级程序(跟踪,计算)
track::caclulate(irrelevant stuff,vector<float> * dvertex) { ... vector<float> pos; ... pos filled after some calculations if(! (dvertex==NULL)) { dvertex = &pos1; }
回到小学,除非我弄乱了上面的东西,这里有一些我尝试过的东西
1
(*dvertex).at(0) float z = (*dvertex).at(0)
2
(*dvertex)[0]
和一堆简单的东西没有编译.由于我不确定如何在主程序中从该向量中获取特定值,所以我很困惑.我甚至认为它可能是if(!(dvertex == NULL))位,因此我将其更改为if(dvertex == NULL)但仍然没有快乐.任何帮助将不胜感激.
*编辑/更新*非常感谢大家的帮助,但我担心我仍然做错了.
所以按照我刚刚传递参考的建议:我这样做了:
主
std::vector<float> dvertex; track.calculate( foo,&dvertex);
二级保持不变(有!空检查)
主
std::cout<<dvertex[0]<<std:endl;
(以及实际使用数据的其他尝试)
非常感谢您对我仍然做得不正确的想法.一切都编译好,当程序达到使用dvertex数据的程度时,程序就会冻结.
编辑:最终修复
在我需要的二级课程中
*dvertex = pos1;
代替
dvertex = &pos1;
解决方法
void f(std::vector<int>* v) { if( v != 0 ) { int n = (*v)[0]; // ok int m = (*v).at(0); // ok int o = v->at(0); // ok } }
但别介意.如果必须更改向量,请使用引用;如果不能,则使用const引用.很少需要通过指针来获取容器.
另外,我建议您检查针对0的指针,而不是NULL,因为有时根据C编译器将NULL定义为(void *)0.但有些人可能会在这里反驳.
iphone – 在视图之间传递变量的最佳方法
Game_info.h
#import <UIKit/UIKit.h> @interface Game_Info : UIViewController { IBOutlet UITextField *groupName; IBOutlet UISegmentedControl *gameType; } @property (nonatomic,retain) IBOutlet UITextField *groupName; - (IBAction) GameTypePicker; - (IBAction) KeyboardHide; - (IBAction) BackBTN; - (IBAction) NextBTN; @end
Game_Info.m
#import "I_Dare_YouViewController.h" #import "Game_Info.h" #import "Game_TDoR.h" @implementation Game_Info @synthesize groupName; // Next Button -(IBAction) NextBTN{ if ([groupName.text length] == 0) { // Alert UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Group Name" message:@"Please enter a group name" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil ]; [alert show]; [alert release]; }else if([groupName.text length] < 3){ // Alert UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Group Name" message:@"Please enter a name longer than 3 characters" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil ]; [alert show]; [alert release]; }else{ Game_TDoR *screen = [[Game_TDoR alloc] initWithNibName:nil bundle:nil]; screen.modalTransitionStyle = UIModalTransitionStyleCrossdissolve; [self presentModalViewController:screen animated:YES]; [screen release]; Game_TDoR *screen1 = [[Game_TDoR alloc] initWithNibName:nil bundle:nil]; NSLog(@"Game_Info: %@",self.groupName.text); screen1.groupNameText = self.groupName.text; [self presentModalViewController:screen1 animated:YES]; [screen1 release]; } }
然后在另一个视图/ .h / .m文件中,我试图进入’groupName’属性.
Game_TDoR.m
#import "I_Dare_YouViewController.h" #import "Game_Info.h" #import "Game_TDoR.h" @implementation Game_TDoR @synthesize groupNameText,Testlbl; - (void)viewDidLoad { NSLog(@"Game_TDoR: %@",self.groupNameText); Nsstring *msg = [[Nsstring alloc] initWithFormat:@"Hello,%@",[self.groupNameText capitalizedString]]; [Testlbl setText:msg]; [msg release]; [super viewDidLoad]; // Do any additional setup after loading the view from its nib. }
所以我想要做的是在第一个视图页面(Game_info)上有一个输入文本框,我试图将其传递给另一个视图页面上的标签(Game_TDoR)
这是NSLog中出现的内容(注意第二页(Game_TDoR)在日志中首先出现.
2011-07-17 00:25:34.765 I Dare You[3941:207] Game_TDoR: (null) 2011-07-17 00:25:34.774 I Dare You[3941:207] Game_Info: Name
问题解决了:
在下一个按钮我需要在移动页面之前添加变量(而不是相反的方式 – 愚蠢的noobish事情要做…)
Game_TDoR *screen1 = [[Game_TDoR alloc] initWithNibName:nil bundle:nil]; NSLog(@"Game_Info: %@",self.groupName.text); screen1.groupNameText = self.groupName.text; [self presentModalViewController:screen1 animated:YES]; [screen1 release]; Game_TDoR *screen = [[Game_TDoR alloc] initWithNibName:nil bundle:nil]; screen.modalTransitionStyle = UIModalTransitionStyleCrossdissolve; [self presentModalViewController:screen animated:YES]; [screen release];
解决方法
1)在Game_TDoR @interface块中声明Nsstring属性:
@property (nonatomic,copy) Nsstring *groupNameText;
(记得在实现块中合成或实现访问器方法)
2)在NextBTN操作中,初始化Game_TDoR实例后,设置属性:
Game_TDoR *screen = [[Game_TDoR alloc] init...]; screen.groupNameText = self.groupName.text;
Javascript:在函数之间传递大对象或字符串被认为是一种不好的做法
所以在代码中它将是这样的:
var response; $.post(url,function(resp){ response = resp; }) function doSomething() { // do something with the response here }
VS
$.post(url,function(resp){ doSomething(resp); }) function doSomething(resp) { // do something with the resp here }
假设resp是一个大对象或字符串,它可以在多个函数之间传递.
解决方法
这意味着将它们传递给函数并不昂贵,因为没有复制.传递给函数的所有内容只是指向原始对象的指针,这是有效的.
您还需要意识到您的第一个方案甚至无法正常工作:
var response; $.post(url,function(resp){ response = resp; }) function doSomething() { // do something with the response here }
因为时间的关系,你不知道何时调用doSomething(). $.post(),因为你显示它是异步的,因此你不知道它何时实际完成.代码中的resp值必须在完成函数中使用.您必须在完成函数中使用它,或者从完成函数调用一些东西并将resp传递给它(就像您的第二个示例).只有这样,您才能获得有关数据可用时的正确时间.
jenkins-在工作之间传递变量?
我在jenkins有两个工作,这两个工作都需要相同的参数。
如何使用参数运行第一个作业,以便在触发第二个作业时使用相同的参数?
答案1
小编典典您可以使用参数化触发器插件,该插件可让您将参数从一个任务传递到另一任务。
您还需要在上游添加从上游传递的此参数。
关于Python:在函数之间传递变量和python函数之间传递参数的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于c – 通过指针在函数之间传递向量、iphone – 在视图之间传递变量的最佳方法、Javascript:在函数之间传递大对象或字符串被认为是一种不好的做法、jenkins-在工作之间传递变量?的相关知识,请在本站寻找。
本文标签: