Implicit conversion to boolean in if and while statements

Rick Johnson rantingrickjohnson at gmail.com
Mon Feb 11 23:00:55 EST 2013


On Monday, February 11, 2013 7:35:23 AM UTC-6, Chris Angelico wrote:
> On Tue, Feb 12, 2013 at 12:13 AM, Rick Johnson wrote:
> > I am vehemently against using more than one "opening seq char" and one "closing seq char". ... we could use start and end tags like:
> >
> >   set{1,2,3}set
> >
> > where "set{" and "}set" are delimiters.
> 
> Interesting. So what you're actually against is the symbols. 

Nothing really. Chars are innocent creatures. They themselves know not of evil. However groups of chars have the capacity to collectively emit evil in the form of density, of which who's demon spawn is "incomprehensibility"!

> Okay. I
> have a bit of a Scheme (rubs hands together with glee) for you. 
> [snip lisping]
> 
> (set 1 2 3)
> (dict 1 2 3 4)   ; implicitly pairs up the arguments
> (tuple 1 2)
> (list 1 2 3 4)    ; This seems like a real good idea.

Well i must admit that this syntax would be beautifully consistent--except for the dict which is far too implicit! I would gravitate to something more like:

(dict 'a':1 'b':2 'name':'fred')

But what happens when we start nesting?

(dict: 
'employees':
    (list 
    'sarah:(dict 
        'age':22,
        'position:'CSR'),
    'john':(dict 
        'age':46,
        'position':'grounds'),
    'albert':(dict 
        'age':85,
        'position':'CEO'),
    ), # end list
'key1':(tuple 1,2,10.1),
'key2':(set 10,20,30),
) # end dict

As opposed to:

{
'employees':[
    'sarah:{
        'age':22,
        'position:'CSR'},
    'john':{
        'age':46,
        'position':'grounds'},
    'albert':{
        'age':85,
        'position':'CEO'},
    ],
'key1':(1,2,10.1),
'key2':set([10,20,30]),
}

But then again, literals are code smell anyway!

  http://en.wikipedia.org/wiki/Code_smell
  
I use literals like anyone else, but i sometimes wonder if i could live without them (probably not string literals though!!!). Many of us can even remember a day (before we learned about recursion) when we would write out gobs and gobs of unnecessary literals that could easily be built with a for loop. I think _most_ literals could just as easily be replicated in code. Consider this alternative approach to building the the dict above:

database = d = {}
d['sarah'] = k = {}
k['age'] = 22
k['position'] = 'CSR'
d['john'] = k = {}
k['age'] = 46
k['position'] = 'grounds'
d['albert'] = k = {}
k['age'] = 85
k['position'] = 'CEO'
d['key1'] = (1,2,10.1)
d['key2'] = [10,20,30]

Not as structured as the literal, but by utilizing indention the message will become clearer. Oh, yeah, better make sure we built that structure correctly!

py> import pprint
py> pprint.pprint(d)
{'albert': {'age': 85, 'position': 'CEO'},
 'john': {'age': 46, 'position': 'grounds'},
 'key1': (1, 2, 10.1),
 'key2': [10, 20, 30],
 'sarah': {'age': 22, 'position': 'CSR'}}

But then the old "for x in LITERAL_SEQ" and the "if this in LITERAL_SEQ" will bite you. Damn literals! Can't live with them, can't live without them.



More information about the Python-list mailing list