What use is of this 'cast=float ,'?
Tim Chase
python.list at tim.thechases.com
Fri Oct 27 17:43:12 EDT 2017
[rearranging for easier responding]
On 2017-10-27 13:35, Robert wrote:
> self.freqslider=forms.slider(
> parent=self.GetWin( ),
> sizer=freqsizer,
> value=self.freq,
> callback= self.setfreq,
> minimum=−samprate/2,
> maximum=samprate/2,
> num_steps=100,
> style=wx.SL_HORIZONTAL,
> cast=float ,
> proportion=1,
> )
> I am interested in the second of the last line.
>
> cast=float ,
The space doesn't do anything. You have a parameter list, so the
comma just separates "cast=float" from the next parameter,
"proportion=1". The "cast=float" passes the "float()" function as a
way of casting the data. In this case, it likely expects a function
that takes a number or string, and returns a number that can be used
to render the slider's value/position.
You could create one that works backwards:
def backwards_float(input):
return -float(input) # note the "-" inverting the interpreted
value
forms.slider(
…
cast=backwards_float,
…
)
> I've tried it in Python. Even simply with
>
> float
This is just the float() function.
> it has no error, but what use is it?
It's good for passing to something like the above that wants a
function to call. The body of the function likely has something like
resulting_value = cast(value)
which, in this case is the same as
resulting_value = float(value)
> I do see a space before the comma ','. Is it a typo or not?
I think it's unintended.
The whole question started off peculiar because outside of a
function-invocation
thing = other,
with the comma creates a one-element tuple and assigns the resulting
tuple to "thing"
>>> x = 42
>>> x
42
>>> y = 42,
>>> y
(42,)
Usually people will be more explicit because that comma is easy to
miss, so they'll write
>>> z = (42,)
>>> z
(42,)
so that later people reading the code know that it's intended to be a
one-element tuple.
-tkc
More information about the Python-list
mailing list