[Tutor] Building input for a function call

Evert Rol evert.rol at gmail.com
Fri Nov 12 20:09:13 CET 2010


> This is a more precise question, the above was after trying different methods,
> and it got a little confusing.
> 
> Why in the below does using in line 12:self.objectsvars =
> 'menuitemhere','menuitemhere','menuitemhere','menuitemhere', not work,
> but placing 'menuitemhere','menuitemhere','menuitemhere','menuitemhere'
> in line 17 directly work?

Singled out the two lines for clarity:
> 12:		self.objectsvars = 'menuitemhere','menuitemhere','menuitemhere','menuitemhere'
> 17:		self.optionmenu = OptionMenu(self.frame,self.var,self.objectsvars)


Because in the first case, you're passing 3 items to OptionMene: self.frame, self.var & a tuple. The tuple itself contains four items, but it is still a single item as passed to OptionMenu.
In the second case, you're passing 6 items total. 

If you want to use self.objectsvars, using the asterisk notation to expand your list or tuple:
OptionMenu(self.frame, self.var, *self.objectsvars)

Though I believe (haven't tried) that that actually doesn't work (depending on how OptionMenu.__init_ is defined), and you may need to pack everything into a single tuple, and expand that:
OptionMenu( *((self.frame, self.var) + self.objects))

So in that case, I'm adding a 2-tuple and a 4-tuple, and then expanding that. I need extra parentheses, otherwise only the 2-tuple gets expanded.

Which of the above two cases you need, depends on how OptionMenu.__init__ is declared:

  def __init__(self, frame, var, *args)

should allow for the first, while

  def __init__(self, *args)

will work in the second case, since frame and var aren't picked up separately when calling the function.


Google and read around for 'Python args kwargs', if this went somewhat over your head. Or just try and play with it some more.

Cheers,

  Evert


> 
> 
> 	def obOpMenu(self):
> 		print 'Dummy' #This is my tony robins help
> 		import time
> 		self.oblist = self.canvas.find_withtag('object')
> 		print self.oblist
> 		self.list = []
> 		for item in self.oblist:
> 			self.itemname = self.canvas.gettags(item)
> 			if self.itemname[1] != 'default':
> 				self.list.append(self.itemname[1])
> 		print self.list
> 		self.objectsvars = 'menuitemhere','menuitemhere','menuitemhere','menuitemhere'
> 		print type(self.objectsvars[0])
> 		self.var = StringVar(self.root)
> 		self.var.set(self.objectsvars[0])
> 
> 		self.optionmenu = OptionMenu(self.frame,self.var,self.objectsvars)
> 		self.optionmenu.grid(row = 1,column = 2)
> 
> 
> Hope that's clearer:)
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list