How do you make functions that take a variable that is not defined? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How do you make functions that take a variable that is not defined? (/thread-12994.html) |
How do you make functions that take a variable that is not defined? - magic - Sep-23-2018 I don't know exactly what this is called toolbar.pack(side=TOP, fill=X)Here above, you can see that TOP, X was passed as a value. (not "TOP", "X") And those are, I assume, predefined by the library that produced that function. How do you make functions that take that kind of value? RE: How do you make functions that take a variable that is not defined? - snippsat - Sep-23-2018 It's called Default Argument Example: def foo(x, y=5): print(f'The sum is: {sum((x, y))}')Now if i use this function i only need to give argument for x parameter.y will always be 5 until give two arguments,then default value get overwritten.>>> foo(4) The sum is: 9 >>> foo(10) The sum is: 15 >>> >>> foo(7, 7) The sum is: 14 >>> foo(100, 599) The sum is: 699 RE: How do you make functions that take a variable that is not defined? - magic - Sep-24-2018 (Sep-23-2018, 03:14 AM)snippsat Wrote: It's called Default Argument That's not default argument. I know what default argument is. Those variables were never defined but passed down into the function in capital letters. RE: How do you make functions that take a variable that is not defined? - Grytpype - Sep-24-2018 Or do you mean the fact that unquoted TOP and X are available as values? These have been defined somewhere. The convention is that any variable in all capitals is a global constant. So somewhere in the library it might define TOP, BOTTOM, LEFT, RIGHT = 1, 2, 3, 4 X, Y = 1, 2or any other convenient values for them. If the code uses expressions like TOP+BOTTOM then they're probably defined as distinct powers of 2, so that all such sums are different values: TOP, BOTTOM, LEFT, RIGHT = 1, 2, 4, 8cross-posted as you were answering RE: How do you make functions that take a variable that is not defined? - stullis - Sep-24-2018 It sounds like you need to preface the variable identifier with the package name. Does this work: toolbar.pack(side=toolbar.TOP, fill=toolbar.X) RE: How do you make functions that take a variable that is not defined? - snippsat - Sep-24-2018 (Sep-24-2018, 09:42 AM)magic Wrote: That's not default argument. I know what default argument is.This how Tkinter dos it,you should mention where code comes from to get better answers. >>> from tkinter import LEFT, TOP, X, FLAT, RAISED >>> TOP 'top' >>> LEFT 'left' >>> FLAT 'flat' >>> X 'x'So toolbar.pack(side=TOP, fill=X) is the same as toolbar.pack(side='top', fill='x') So it could work fine as default argument to a function. With toolbar.pack() they are passed in as key word arguments(**kwargs).If look at method parameters in Tkinter code. def pack_configure(self, cnf={}, **kw):Example: >>> def my_function(**kw): ... print(kwargs) ... >>> my_function(a=123, b="hello") {'a': 123, 'b': 'hello'} RE: How do you make functions that take a variable that is not defined? - gruntfutuk - Sep-24-2018 Uppercase is typically used to indicate constants that have been set elsewhere. When you call a function, you can specify the names of the parameters (i.e. the names in the function definition), for clarity (and to ignore ordering of arguments). Arguments can be either actual values or variables (or constants, which are just variables). def func(paramx, paramy, paramz=defaultz, ...): func(paramx=valuex, paramy=valuey, paramz=valuez, ...) Note. Sometimes actual values will not give you desired outcomes such as when a reference to a mutable object is required by the function that is intended to mutate the object. Using an actual value (e.g. an in-situ list) in such a case will mean there is no reference to the mutated object after the call is completed. |