A formal definition for the term "pythonic" would be largely impossible.
It's as impossible as a definition for "aesthetic" or "convenient" or "delightful".
It can't be less subjective than it is now.
Why not?
Because Python is merely as aesthetic judgement based on the opinions of a fairly large number of people. The GvR points are respected, but do not have the force of law. Many people write code for the Python world, and their various inputs influence other developers. It's a vast social network of people all liking (and disliking) the code they read.
Ultimately I would like to get closer to more logical or mathematical interpretation of the matter
You would do well to start with studying art, aesthetics and philosophy first. Then, after mastering aesthetics and morals, switch to ontology so that you can encode what you learned about truth and beauty. Once you've finished that, you'll have a formal definition of Pythonic along with formal definitions of all the various subjective values that we hold.
axioms ... built/reformulated from "Zen of python"
It's a poem. It's value is entirely aesthetic. What more evidence does one need that Pythonic is a value judgement based on depth of experience?
Well, that kind of depends on what you are willing to call a "data structure." According to Wikipdia, a data structure is simply a "particular way of storing and organizing data in a computer so that it can be used efficiently." So, therefore, classes would be a form of data structure, and classes are very widely used in Python. But, for the sake of this answer, I will assume you are more interested in what you might learn about in a data structures and algorithms class (e.g. trees, linked lists, queues, hashes, etc...).
Now, because of Python's pseudo-code-like syntax it can be a very useful language for implementing data structures in. If for no other purpose than just to aid in understanding these basic concepts. For example, when I first learned about linked list I decided to implement them in Python:
class node:
def __init__(self, data = None, is_header = False):
self.data = data
self.next = None
def __str__(self):
return "|{0}| --> {1}".format(str(self.data), str(self.next))
class linked_list:
def __init__(self):
self.header = node("!Header!", True)
def add_node(self, add_me):
current_node = self.header
while 1:
if current_node.next == None:
current_node.next = add_me
break
else:
current_node = current_node.next
def print(self):
print (str(self.header))
Now, this isn't a perfect example, nor is it even a totally proper implementation of a linked list, but it goes to illustrate something:
Python's simple syntax can be helpful in understanding data structures
Another example would be a priority queue that I built back in the day:
class priority_queue:
def __init__(self):
self.element = []
self.priority = []
def enqueue_with_priority(self, me, priority):
where = 0
for i in range(len(self.element)):
if not priority < self.priority[i]:
where = i
break
self.element.insert(where, me)
self.priority.insert(where, priority)
def dequeue_highest(self):
return self.element.pop(0)
def print(self):
for i in self.element:
print i
Again, not a perfect implementation but it illustrates another benefit of coding a data structure in Python:
Python is useful in prototyping data structures to optimize them for lower-level programming languages
Looking back on this code I see flaws in my implementation. The Python code, however, tends to be short and sweet. So if I wanted to implement a data structures in a lower-level language (such as a c-style language) I could first generate a quick Python prototype and then optimize later.
Finally, I think Python can help in the development of data structures, because:
In Python, development is quick, mistakes are allowed and you can try things out.
Imagine you are building a hash-table-like data structure, in a strongly-typed, compiled language you would usually try things out in an IDE, then have to compile and run it. In Python, you can just pull up IDLE, iPython or the Python interpreter and just try things out! No need to recompile for each little change to the hash function you want to try -- just plug it into the interpreter.
So, in conclusion, I guess what I'm saying is that I agree with you: there's not a lot of practicality in building your own data structures (since most anything you may want has already been implemented and optimized). However, for me, there is a lot of educational benefit (because of Python's ease of syntax), as well as a lot of creative and developmental freedom (due to Python's low-constraint, EAFP design).
It is important to note that although python (through its wide-reaching library) provides many standard data structures, a "data structure" (by definition) can be almost anything. So, in Python as well as any other language we may use to solve non-trivial problems, we are going to need to define new data structures. Therefore, it is quite arguable that serious Python developers create custom data structures just as much as serious developers in other languages do.
Best Answer
SimpleNamespace
is basically just a nice facade on top of a dictionary. It allows you to use properties instead of index keys. This is nice as it is super flexible and easy to manipulate.The downside of that flexibility is that it doesn't provide any structure. There is nothing to stop someone from calling
SimpleNamespace(x=ax, y=ay)
(anddel a.z
at some point later). If this instance gets passed to your function, the exception occurs when you try to access the field.In contrast,
namedtuple
lets you create a structured type. The type will have a name and it will know what fields it is supposed to have. You won't be able to make an instance without each of those field and they can't be removed later. Additionally, the instance is immutable, so you will know that the value ina.x
will always be the same.It's up to you to decide if you need the flexibility that
SimpleNamespace
gives you, or if you prefer to have the structure and guarantees provided bynamedtuple
.