20
faheel
75d

Came across this when refactoring colleague's code:

Comments
  • 10
    For those here to learn:

    if kwargs is not None:
    self.file = kwargs.get('file')
  • 5
    Forgive me, I do not use python very much but I thought you could just subscript kwargs?

    kwargs['file']
  • 5
    Okay just tested it because I've got a million things to do but I NEED ANSWERS and...
  • 5
    @AlgoRythm This will raise an KeyError if file is not a key. The above solutions do not do that.
  • 3
    @AlgoRythm `dict.get(key, default_value=None)` is simply a wrapper method that returns `kwargs[key]` if the key exists, otherwise the `default_value` specified (which is `None` by default)
  • 1
    Your colleague's name is "Rube Goldberg", isn't it? 🤦‍♂️
  • 2
    @faheel That makes sense. I like in Lua you can do

    your_list['key'] or 'default value'

    Reads so much nicer 😍
  • 3
    Just realised that `kwargs` will implicitly be an empty dict when not specified, so even the first if condition is not required.
  • 1
    Writing the condition "!=null" as "is not none" fills me with irrtational hatred.
  • 1
    @cundy oh, you could write "!= None" in Python just as well. This is something different, though: "!=" tests for equality, "is not" OTOH for identity. This is correct here, since "None" is a singleton.
  • 1
    If kwargs and kwargs.get(“file”,None):
    self.file = kwargs[“file”]
  • 1
    Oneliner.

    self.file = kwargs.get('file', self.file)
  • 0
    @galileopy will raise exception if kwargs is None :p
  • 0
    @dalastTomCruise why the double access? You can drop the "and get()", it doesn't add anything
  • 1
    @deadPix3l actually, if you remove the get it will cause an KeyError exception... it was more for not assigning self.file a value if “file” isn’t a key. But I guess:

    If kwargs:

    self.file = kwargs.get(“file”, self.file)

    But still you’re redundantly reassigning when there’s no “file” key present
  • 2
    @dalastTomCruise kwargs from function signatures are never None. If no keyword args are passed, kwargs is an empty dict.
  • 0
    @VaderNT still the conditional will work even if it’s an empty dict. Empty dict will evaluate to False regardless.
  • 2
    @dalastTomCruise what I mean is when you said "will raise exception if kwargs is None", that cannot happen.
  • 2
    @VaderNT oh I got you, you were referring to when I said it’ll throw an exception. Very true
  • 0
    @AlgoRythm Wait.
    What if you don't write the or thingy?
    Does it throw error?
  • 0
    @Konsole No, it just returns nil
  • 1
    The thing in my perspective is.

    self.file = kwargs.get('file', self.file)

    Is just clearer. It explicitly says keep file unchanged unless there's a kwarg named file.

    But that's just me.
  • 0
    @AlgoRythm Isn't that bad then...?
    What if it leads to unexpected behavior
  • 0
    @Konsole That's exactly what Python's dict.get does?
  • 0
    @AlgoRythm In dict.get we choose to get unexpected behavior. We can just do D[ ] to get error throws.
    Does lua provide such an option
  • 0
    @Konsole What purpose does an error have over returning nil?
  • 0
    @AlgoRythm a nil can cause other parts to misbehave if not handled properly. Also makes harder to pinpoint problems during debugging
  • 0
    @Konsole I disagree. Nil just means the data isn't there. You don't need an error for that.

    This is now Lua works for all things. There is no such thing as "undefined". If you try to use a variable that doesn't exist, it doesn't throw an error, it just gives you nil.

    It can be confusing at first and definitely makes the autocomplete feature of your IDE more useful but it isn't wrong, it's just a slightly different approach.
  • 0
    @AlgoRythm What if some other part expects some value but instead gets nil. That may cause it to fail or worse misbehave
  • 0
    @Konsole if a procedure expects a value but gets nil, that is the fault of the programmer. That's why you can also do default values before you start.

    variable = variable or "some default value";
  • 0
    @AlgoRythm technically this is different in Python, where None is a valid value different from "not set". Given a dict {1: None}, d[2] raises an exception while d[1] doesn't. Tbh I like Lua better in this "billion dollar mistake" regard.

    "var = var or default" has the pitfall that things like the empty string evaluate as False in Python. d.get() won't have that problem.
  • 0
    @faheel Still leaves you with the necessity to check if self.file is None afterwards and handle that though.
Your Job Suck?
Get a Better Job
Add Comment