The term 'dot-notation' as used in Python seems to be used for two groups of meanings in Python:
- 1) most are JavaScript/JSON-inspired coding practices (e.g. expectations that nested object or dict references to undefined (chains of) keys should automatically recursively create all the object attributes/dict keys in the chain, in the manner of defaultdict/autovivification) ...
- 2) whereas some other mentions are simply "when/should I call a method on an object vs referencing it via attributes, or via a property". Conversely these are existing builtin Python language syntax.
- [EDIT: per @jpp's answer, I'm not proposing inventorying all the individual places 'dot notation' is used for builtin Python syntax. Just separating meaning 2) as a group from meaning 1)]
- What seems to be lost on non-Python programmers is that meaning 1) of "[implementing] dot notation" in Python is largely a foreign thing which the language does not natively implement (yet is well-defined in JS), so unlike JS we have to implement it piecewise ourselves, and that requires that we're clear what the defined behavior(s) are.
- Some questions are only about objects, some only about dicts, some about both, some about the difference between the two. Some are only about old-style/2.x objects and have been obviated by 3.x
- Some questions are about namespaces
- Some questions are about loading/dumping JSON at the same time or in the same code as getting/setting the multiply-nested dicts or objects
- Some uses ignore or break inheritance e.g. by messing with
__getattribute__
or not calling super. - Moreover, doing this across 3.x and 2.x, and in a performant way, and using builtins where possible, are all considerations.
So the question is: How can we define and organize the main distinct meanings of 'dot-notation' in Python? and aggregate questions on the first meaning, which is "JS-style automatic getting/setting of nested chains of object attributes/dict keys(/namespaces)". ** Following are some questions **not to endorse their propositions, just to illustrate that this topic is swimming in duplicates, as a motivation why we should clean this up:
- First meaning: "JS-style automatic getting/setting of nested chains of object attributes/dict keys(/namespaces)" [Note [1]]
- What are the differences between Python Dictionaries vs Javascript Objects?
- Python dict using dot notation and chaining
- How to use a dot “.” to access members of dictionary?
- How to use dot notation for (accessing) dict in python? - Checking multiple hierarchical dict keys in a chain, and only creating keys where they do not already exist: Checking a nested dictionary using a dot notation string
- Set Python dict items recursively, when given a compound key 'foo.bar.baz'
- Python recursive setattr()-like function for working with nested dictionaries
- Javascript style dot notation for dictionary keys unpythonic?
- How to create objects on the fly in python? ; answers include namespaces, tuples using
type
, NamedTuples - Easily dumping variables from/to namespaces/dictionaries in Python ; reviews 9 links to other questions addressing dot-access of dynamically-defined variables, but not 'dumping'.
- HackerNews article 5/2017: Box: Python dictionaries with recursive dot notation access covering third-party library Box
- Second meaning: Questions about native Python syntax for objects, attributes etc.:
- Other unrelated misc mentions in native Python syntax of 'dot notation', referring to methods, module imports and various other things:
- Why do some methods use dot notation and others don't?
- Python importing (module] with dot notation
- A severe misunderstanding stemming from a bad blog: Do Python methods that use dot notation e.g.
.sort()
work on types other than string? - A misunderstanding from seeing a method call on the float
1.
What is1..__truediv__
? Does Python have a .. ("dot dot") notation syntax? - Access Pandas Series item with dot notation like DataFrame
[NOTE [1]: Strictly, JSON keys are allowed to contain whitespace but Python attribute names aren't. So implementing this via attributes isn't 100% JSON compatible.]
self.__dict__
(instead of via getattr) or reduce performance, which are plain bad, and should be closed/merged explaining why they're bad. Figuring all that out took me time, and search function won't fix any of that.