Suspecting the interpreter.



  • [Python] 
    def process_upload(self, **keywds):
    frm_batch_id = keywds['frm_batch_id']
    frm_product = keywds['frm_product']
    additional_info = keywds['additional_info']
    frm_sequence_id = keywds['frm_sequence_id']
    files_for_upload = keywds['files_for_upload']
    num_files = keywds['num_files']
    frm_try = keywds['frm_try']
    Note for non-pythoneers: **foo puts all arguments given by keyword in a Hash, so this is nearly
    equivalent to just write them in the function-header but ignores all extra-arguments silently.
    Not a big WTF, just one of those `What was he thinking?`.

     
     
     


  •  Is there a parameter limit that's less than he needed?



  •  no, theres no limit except the one given by sanity;)



  • Actually it makes perfect sense to me. I've done something similar myself in PHP.

    If the data comes from a webpage POST request, then it should be already in a hash and there's probably no need to use it before calling this function.

    It's much more convinient to call a function with only one argument, than to write out 20+ arguments in many places and to make sure that they're in the right order.

    What if the client wants an extra field on the form? If this function is used in a lot of places then you would have to add an extra parameter to all those places.

    But this way you only need to fix the function definition. 



  • What julmu said. But in different words.



  • @Zecc said:

    What julmu said. But in different words.

    TDWTF!



  • @julmu said:

    Actually it makes perfect sense to me. I've done something similar myself in PHP.

    If the data comes from a webpage POST request, then it should be already in a hash and there's probably no need to use it before calling this function.

    It's much more convinient to call a function with only one argument, than to write out 20+ arguments in many places and to make sure that they're in the right order.

    What if the client wants an extra field on the form? If this function is used in a lot of places then you would have to add an extra parameter to all those places.

    But this way you only need to fix the function definition. 

     

    You misunderstand: Python, unlike PHP, has real keyword arguments; there's no need to fake them via a hash.  Python always lets you specify your function parameters by name in arbitrary order.

    Also, the above function isn't taking a hash as an argument as you seem to think; it's taking a bunch of individual named parameters and mapping them into a hash . . . and then immediately pulling them out into separate variables anyway.



  • @julmu said:

    If the data comes from a webpage POST request, then it should be already in a hash and there's probably no need to use it before calling this function.
    Doesn't work that way. It works like this:

    If you call obj.process_upload(name='Steve', color='purple', freshness_rating=2), all those named parameters (name, color, and freshness_rating) that aren't explicit arguments in the function definition (which none of them are) get rolled up into the dictionary (what you call a hash) keywds. This is not the same as simply passing a dictionary to the function. You can make it the same by unrolling the dictionary when you pass it: obj.process_upload(**hash).



  • Oops.

    Yes, you're right. Maybe they like to specify the parameters in different orders?

    Or maybe they like being verbose. This way in every function call you always preceed the parameters' values with their names, which may be a good idea when you have this many.

     

     



  • @Zecc said:

    Or maybe they like being verbose. This way in every function call you always preceed the parameters' values with their names, which may be a good idea when you have this many.

     

     

    Like said, python already has that feature. Only thing that this adds it that it enforces you to work this way.



  • @Daid said:

    Like said, python already has that feature.
    Ok. I didn't know you could do that for "normally declared" parameters too.

    def test(arg1, arg2):

        print 'arg1 = ', arg1, ', arg2 = ', arg2

    test(arg1 = '1', arg2 = '2')  # prints "arg1 = 1, arg2 = 2"

    test(arg2 = '2', arg1 = '1')  # prints "arg1 = 1, arg2 = 2"

    Now I know.



  • less than optimal, but it didn't make me say wtf

    he should've used **kwargs instead of some self invented name though


  • Considered Harmful

    Maybe he has a collection of values where some are unrelated to the function, and just wanted to pass the whole thing in rather than only the necessary values.



  •  then he could have specified his arguments and **kwargs and ignore kwargs...


  • Considered Harmful

    Well, then, maybe...

    Okay, you got me. WTF?

    It seems fairly harmless, though. It's just needlessly verbose.



  • @joe.edwards said:

    It seems fairly harmless, though. It's just needlessly verbose.

     

    Unfortunately, it's not really verbose, more voicing something under its beard. Just consider that style in a large project: for each and every function / procedure / method you call, you have to look into the *implementation* to find out which parameters it actually expects.



  • @ammoQ said:

    @joe.edwards said:

    It seems fairly harmless, though. It's just needlessly verbose.

     

    Unfortunately, it's not really verbose, more voicing something under its beard. Just consider that style in a large project: for each and every function / procedure / method you call, you have to look into the implementation to find out which parameters it actually expects.


     Then document it in the function string:

    def doSomething(**kwargs):

        """ This function expects x, y, and z """


     



  • @Soviut said:

     Then document it in the function string:

    def doSomething(**kwargs):

        """ This function expects x, y, and z """
    Putting it the function string still isn't as good as enforcing it in the code. If the function expects x, y, and z, then make them arguments. There's no reason you can't collect everything else passed to it in a dictionary.



  •  Exactly the same thing was done by a intern at my office as well :) (the self invented name for a useless **kwargs.


Log in to reply