News for hackers. Stuff that matters.
Re:Asterisks in Python by sooheon on Thursday October 11, 2018 @07:06PM (#18197748)
Now how much nicer would it be if even more things were functions, rather than special syntax like here? This can be done with an `apply`.
Re:Asterisks in Python by nemetroid on Thursday October 11, 2018 @07:30PM (#18197874)
I think the star syntax is a lot nicer than `apply`. Python actually had an `apply` builtin, but it was deprecated in Python 2.3.
Re:Asterisks in Python by sooheon on Thursday October 11, 2018 @08:14PM (#18198095)
Apply, and first class / higher order functions in general are good in languages where they compose well. Python's * won't compose well.
Re:Asterisks in Python by quietbritishjim on Friday October 12, 2018 @01:00PM (#18202819)
To add a small footnote to the existing replies to this comment: A related, but slightly different, function in the `functools` module (part of the Python standard library) is the `partial` function. It is like apply() but returns a function instead of calling the function and return its return value:

    f1 = partial(f, 1, 2)
    f2 = partial(f1, 3, 4)
    f2(5, 6)  # equivalent to f(1, 2, 3, 4, 5, 6)
It also supports kwargs.
Re:Asterisks in Python by syrrim on Friday October 12, 2018 @01:47AM (#18199507)

    def apply(f, a):
        return f(*a)
Re:Asterisks in Python by int_19h on Thursday October 11, 2018 @08:53PM (#18198273)
How wouldn't * not compose as well as apply?
Re:Asterisks in Python by maybecorrect on Thursday October 11, 2018 @08:39PM (#18198221)
It is a function - print() is being called as a function, taking a number of positional, arguments followed by named argument. I don't think you're correct about this being special syntax, except for the '*' is being used to expand a list into the positional arguments.
Re:Asterisks in Python by mclehman on Thursday October 11, 2018 @10:11PM (#18198592)
I believe they meant replacing the special * syntax with a use of apply so that there was no special syntax to achieve that functionality at all.
Re:Asterisks in Python by wodenokoto on Saturday October 13, 2018 @12:52PM (#18208849)
I'm not the sharpest functional programmer in the cohort, but isn't `apply` a parallelizable for-loop?

    apply(l, print)
is the same as

    for el in l:
      print(el)
which is a number of print statements, rather than 1 print statement with a number of arguments.