I've been programming with Python for some time and I see the virtues of it to some extent. I've decided to go back and read Learning Python to help myself be more Python-ic in my Python programming - my code tends to look like C++ written in Python.
Early in the book there is a discussion of dictionaries and the following example:
<snip>
>>> rec = {'name': {'first': 'Bob', 'last': 'Smith'},
'job': ['dev', 'mgr'],
'age': 40.5}
Here, we again have a three-key dictionary at the top (keys “name,” “job,” and “age”),
but the values have become more complex: a nested dictionary for the name to support
multiple parts, and a nested list for the job to support multiple roles and future expansion.
We can access the components of this structure much as we did for our matrix
earlier, but this time some of our indexes are dictionary keys, not list offsets:
>>> rec['name'] # 'name' is a nested dictionary
{'last': 'Smith', 'first': 'Bob'}
>>> rec['name']['last'] # Index the nested dictionary
'Smith'
>>> rec['job'] # 'job' is a nested list
['dev', 'mgr']
>>> rec['job'][-1] # Index the nested list
'mgr'
>>> rec['job'].append('janitor') # Expand Bob's job description in-place
>>> rec
{'age': 40.5, 'job': ['dev', 'mgr', 'janitor'], 'name': {'last': 'Smith',
'first': 'Bob'}}
Notice how the last operation here expands the nested job list—because the job list is
a separate piece of memory from the dictionary that contains it, it can grow and shrink
freely (object memory layout will be discussed further later in this book).
<snip>
To me this just looks awful! It seems incredibly arbitrary and fraught with peril. If I were to develop a toolkit for others to use or if someone had to follow me on a piece of code like this, I would imagine I would be shot.
Am I just not open to the experience yet and too jaded from writing mostly C/C++?
Anybody else find Python overly loose and maybe even a bit sloppy?
-Kirk
Python dictionaries - sloppy??
What particular perils were you thinking of?
There is quite a culture shock going from static languages to dynamic languages. Good coding practices are different -- in particular, defensive coding practices are different. You don't spend as much time nailing down datatypes, but you spend a lot more time writing unit tests.
There is quite a culture shock going from static languages to dynamic languages. Good coding practices are different -- in particular, defensive coding practices are different. You don't spend as much time nailing down datatypes, but you spend a lot more time writing unit tests.
Oh no! My introductory C++ programming book uses vectors of vectors in an example! That seems incredibly inefficient! C++ MUST BE HORRIBLE!
Sneftel - it just seems to me that all of this is too arbitrary. My concern is code maintenance over the long term. It seems this is more difficult to sort out than what I'm used to, but that may simply be because I'm not used to it yet.
SiCrane - What are you talking about? vectors of vectors make PERFECT sense - I use them all the time. (Seriously. I do.) Double and triple pointers, too. 8^P
SiCrane - What are you talking about? vectors of vectors make PERFECT sense - I use them all the time. (Seriously. I do.) Double and triple pointers, too. 8^P
Quote:Original post by kirkdStill a little vague. These sorts of things are easier to illustrate with examples. What's a bug that might crop up long-term?
Sneftel - it just seems to me that all of this is too arbitrary. My concern is code maintenance over the long term. It seems this is more difficult to sort out than what I'm used to, but that may simply be because I'm not used to it yet.
I don't really have a concrete example to provide you. It just seems odd to me that we're storing all different data types with the option of other data types being added in a rather arbitrary manner. If I were trying to access the content of a dictionary coded like this, it seems to me it would be much more confusing than if this were wrapped up in a special function class. Again, it may just be that I'm not used to it. I'm not trying to start a C/C++ vs Python flame war.
So you decide to call entire language sloppy because of one example by one author in an introductory book, despite the fact that you can't actually point out any problems that would occur. Are you familiar with the term "jumping to conclusions"?
I think your argument (which is valid) is that such a thing is simply unreadable and unclear.
I agree. To be honest, yes, python is a scripting language and yes you can do things like this, but I never understood for the life of me why you would /want/ to when proper data structures are first class language features of the language. Good object oriented design and code is good in any language, and I think this is a good example of a situation where just because the language supports implementing a class as a named dictionary doesn't mean you should.
Consider this example, which is valid C++
This code is completely legal, and it is even a valid design from an abstract math point of view (a vector3d is a sequence of floats,ordered by their label). But it is COMPLETELY BACK-ASSWARDS for anyone who knows how C++ works and knows what SHOULD be done here.
The point is that nobody in C++ implements complex (or even simple) data structures as lists of groupings of tuples unless they are absolutely terrible at design or unless the data structure they are implementing is actually best described as a tuple.
I see no reason why it would be "better" design to do the opposite in python, just because of the fact that you can.
I agree. To be honest, yes, python is a scripting language and yes you can do things like this, but I never understood for the life of me why you would /want/ to when proper data structures are first class language features of the language. Good object oriented design and code is good in any language, and I think this is a good example of a situation where just because the language supports implementing a class as a named dictionary doesn't mean you should.
Consider this example, which is valid C++
typedef std::map<std::string,float> Vector3d;float dotProduct(const Vector3d& a,const Vector3d& b){ return a["x"]*b["x"]+a["y"]*b["y"]+a["z"]*b["z"];}
This code is completely legal, and it is even a valid design from an abstract math point of view (a vector3d is a sequence of floats,ordered by their label). But it is COMPLETELY BACK-ASSWARDS for anyone who knows how C++ works and knows what SHOULD be done here.
The point is that nobody in C++ implements complex (or even simple) data structures as lists of groupings of tuples unless they are absolutely terrible at design or unless the data structure they are implementing is actually best described as a tuple.
I see no reason why it would be "better" design to do the opposite in python, just because of the fact that you can.
It's not A+ coding style, but it does illustrate the heterogeneity of Python's dicts. It would probably make more sense to use an object for this kind of thing, but in their absence, a dictionary will do just fine. I presume that you haven't come across how classes and objects are dealt with in Python yet, though.
So, be patient and try to bottle your complaints until you have more context.
So, be patient and try to bottle your complaints until you have more context.
Whoa, whoa, whoa! I'm not trying to trash the whole language, SiCrane. Slow down a bit there - go back and re-read my original post. I think we can all agree that the cited example is outside the realm of good coding practices. In my experience, which I'm certain is not as much as others' here, I see code like this often, and to me it seems like maintaining this sort of thing would be a nightmare.
Also note that I fully admitted in my original post that I may just not be used to the concepts of Python yet. I was just putting some feelers out there.
Steve132 and the_edd - thank you both for the responses. As I mentioned above, a lot of the code that I've seen (mostly free stuff and code from other people I know) have felt much like this snip. In my admittedly limited experience, I keep running into code that I need to deal with that is like this. I'm glad to hear from those with more real world experience that it can get better.
Perhaps I just need to experience better code.
Also note that I fully admitted in my original post that I may just not be used to the concepts of Python yet. I was just putting some feelers out there.
Quote:Original post by Steve132
I never understood for the life of me why you would /want/ to when proper data structures are first class language features of the language.
...
The point is that nobody in C++ implements complex (or even simple) data structures as lists of groupings of tuples unless they are absolutely terrible at design or unless the data structure they are implementing is actually best described as a tuple.
Quote:Original post by the_edd
it does illustrate the heterogeneity of Python's dicts. It would probably make more sense to use an object for this kind of thing,
Steve132 and the_edd - thank you both for the responses. As I mentioned above, a lot of the code that I've seen (mostly free stuff and code from other people I know) have felt much like this snip. In my admittedly limited experience, I keep running into code that I need to deal with that is like this. I'm glad to hear from those with more real world experience that it can get better.
Perhaps I just need to experience better code.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement