6 ms·
I don't get it, isn't that exactly what it's doing here?
by hmry 6y ago
I don't get it, isn't that exactly what it's doing here?
- magnusmundus 6y agoIt's mutating (not shadowing) `not_found` with the value of `status`. That can cause trouble if you rely on `not_found` keeping the initial value later somewhere. Which you would, e.g. with the global constant of `NOT_FOUND`. Honestly I think the issue is so troublesome only if there's a single case to match, though. With more expected cases it should cause pretty obvious bugs (easy to catch).
- deleted 6y ago[deleted]
- wendyshu 6y agoReassigning not mutating
- Dylan16807 6y agoReassigning to a variable is how you mutate a variable, isn't it? The value being [im]mutable is a different topic entirely.
- deleted 6y ago[deleted]
- carapace 6y agoThere aren't actually "variables" in Python in the sense of named values, instead there are namespaces where values are stored and looked up under string identifier keys. (Just to add spice, some values, namely function and class objects, do get their names embedded in them, but this is only used for debugging. They are stored in enclosing namespace dicts like other values.): >>> def f(): pass >>> g = f >>> g <function f at 0x000001B4A686E0D0> >>> locals() {'__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__doc__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__name__': '__main__', '__package__': None, '__spec__': None, 'f': <function f at 0x000001B4A686E0D0>, 'g': <function f at 0x000001B4A686E0D0>}
- Dylan16807 6y agoWait, what's your definition of variable? Because what you're describing fits perfectly into what I would call a variable. There is a mapping from an identifier to a slot that can store a value (or reference), and that mapping is stored in a specific scope. I would call that mapping a variable. I'm not sure exactly why you mentioned objects that have names embedded in them. Is that relevant to the definition you're using?
- carapace 6y ago> Wait, what's your definition of variable? With full formality, the definition of "variable" depends on context. In assembly language and C variables are names for storage locations in RAM, in Python they are name bindings in a dict data structure. One distinction we could make is whether the names are compiled away or kept and used at runtime. In any event, the important thing is to keep clear in one's mind the semantics of the language one is using. In Python you have values (objects of various types: ints, strings, tuples, lists, functions, classes, instances of classes, types, etc.) some of which are mutable and others are immutable, and you have namespaces: dicts that map strings to values. These namespaces have nothing to do with the location of the values in RAM. So it doesn't really make sense in Python to speak of "mutate a variable", you can mutate (some) values, and rebind values to names (new or old). > I'm not sure exactly why you mentioned objects that have names embedded in them. Is that relevant to the definition you're using? Not really, it's just another little point that sometimes confuses some people when they are coming to grips with the idea that in Python values are not associated with names except by namespace bindings. There are some values (function and class objects) that do get associated with names.
- Dylan16807 6y agoI suppose, but it's very strange to say "Assigning to a variable is classified as mutating the variable in some languages but not others, even though the underlying mechanism works exactly the same way." Shouldn't terms like this be cross-language?