Accidentally obfuscated code



  • Code is as follows:

    interpretedData={'location 1':{},'location 2':{},'location 3':{},'location 4':{},'location 5':{},'location 6':{}}

    interpretedData gets further initialized elsewhere.

    SetData is called with one parameter (data) by a C++ application.

    def SetData(data, d_list=interpretedData):
    # data[0] is the name
    # data[1] is the location
    # data[2] is the amount left
    d_list[str(data[1])][str(data[0])]=str(data[2])
    interpretedData=d_list
    global interpretedData

    I ran into this gem today. It's a wonderful mindfuck, that took me a few minutes of staring before I realized why it worked in the way that it did work. I'll leave the simplified SetData() function as an exercise for all your entertainment. :)

    A cookie for the person who explains why it essentially is a double mindfuck.



  • interperetedData is set to d_list which is already interperetedData?

    It unnecessarily does not have 3 paramaters, instead of having a f-ed up way to pass an array. The data needs to be cast to a string, is it not a string, is it arbitrary?

    data[1] is a constant "location" data[0] is an integer with a space prefix?

     



  • The global statement being after the assignment is a mistake, and Python will warn you of this, like so:

    <stdin>:8: SyntaxWarning: name 'interpretedData' is assigned to before global declaration

    If interpretedData is never assigned to a different object and the d_list argument is really never used, then it can be simplified to this:

    def SetData(data):
    interpretedData[str(data[1])][str(data[0])]=str(data[2])
    Otherwise, you can't make any other changes without modifying its behavior.



  • FUCKING EDIT TIME LIMITS

    Let's just pretend the forum software isn't utter shit, and this is what my post says now:

    @The Real WTF said:

    The global statement being after the assignment is a mistake, and Python will warn you of this, like so:

    <stdin>:8: SyntaxWarning: name 'interpretedData' is assigned to before global declaration

    If the d_list argument is really never used, then it can be simplified to this:

    def SetData(data):
    interpretedData[str(data[1])][str(data[0])]=str(data[2])
    Otherwise, you can't make any other changes without modifying its behavior.



  • Funny, I thought I knew that about global and was pretty sure it had to be right after the def, but couldn't find it in the docs (docs say 'has to be in same code block') so I left it where it was. I merely changed some variable names around. 'The Real WTF' indeed gave the correct simplification.

    About the meaning of the indices [0..2], they don't really matter too much. I'm not sure why I left those comments in... extra obfuscation perhaps. The actual code isn't mine, and while the use of str() isn't the wtf, the person who wrote it both misunderstood assignments as well as a common problem with default parameters in Python. I'd hope I won't have to explain -how- they were misunderstood.. and at the same time actually managed to produce working code.



  • @The Real WTF said:

    FUCKING EDIT TIME LIMITS

    Let's just pretend the forum software isn't utter shit, and this is what my post says now:

     

    Some of us just proofread our posts before we click submit.  Not dlikhten, but some of us.  If it were up to me, there'd be no edit ability at all and people might actually stop to think before they posted.



  • @morbiuswilters said:

    If it were up to me, there'd be no edit ability at all and people might actually stop to think before they posted.
    A better system would force people to use preview by only having the post button showup on the preview page.



  • @Lingerance said:

    A better system would force people to use preview by only having the post button showup on the preview page.
     

    Except that would penalize those of us who actually read before we post.

    I prefer questioning the intelligence of people who post dlikhten-speak. Much more fun.



  • @dlikhten said:

    It unnecessarily does not have 3 paramaters, instead of having a f-ed up way to pass an array.
     

        def SetData((data_name, data_location, data_amount_left), d_list=interpretedData):
    ...


  • @Lingerance said:

    @morbiuswilters said:
    If it were up to me, there'd be no edit ability at all and people might actually stop to think before they posted.
    A better system would force people to use preview by only having the post button showup on the preview page.

    Clicking the "Preview" tab on some versions of Safari not only fails to actually show a preview, but totally disables the "reply" page. I wouldn't be surprised to find that this is the case in other browsers as well.



  •  @Carnildo said:

    Clicking the "Preview" tab on some versions of Safari not only fails to actually show a preview, but totally disables the "reply" page. I wouldn't be surprised to find that this is the case in other browsers as well.

    Works fine. FF3b5



  • @Carnildo said:

    Clicking the "Preview" tab on some versions of Safari not only fails to actually show a preview, but totally disables the "reply" page. I wouldn't be surprised to find that this is the case in other browsers as well.

    Safari?  Really??  Why aren't you using a real browser like FF?  You might as well complain that your solar calculator can't make phone calls.



  • @morbiuswilters said:

    @Carnildo said:

    Clicking the "Preview" tab on some versions of Safari not only fails to actually show a preview, but totally disables the "reply" page. I wouldn't be surprised to find that this is the case in other browsers as well.

    Safari?  Really??  Why aren't you using a real browser like FF?  You might as well complain that your solar calculator can't make phone calls.

     

    I do, but noone listens.



  • Yep, I remember back before the upgrade when everyone was afraid to preview because it usually didn't let you post afterward.



  • @dlikhten said:

    I do, but noone listens.
    Oh!  So that's why my calculator has been making weird noises for the last few years...


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.