dimanche 22 février 2015

Printing in Python -- inherently functional?


Learned Python for a project at work (which is cool, b/c now I can fiddle with Anki!). In general, I really like the language. Not as intense as Perl, and although I haven't played with its libraries very much so far, it seems every bit as powerful as Perl with less thinking.


One of the very few things I'm NOT fond of is Python feels like it can't make up its mind between whether its procedural or object oriented. One example of this which is causing me grief is printing. For example, this causes an error:



f = 5.0
print 'The value is ' + f


You need to explicitly convert f to a string. Or else resort to one of the least object-oriented paradigms since 1969:



f = 5.0
print 'The value is %f' % f


What's going on here? I claim that if Python won't print that first snippet, it's only because Python isn't trying hard enough, not because it can't. Let's say that numerical values in Python aren't "objectified" like Java's Integer or Double classes. Maybe they're simply numbers that don't know how to return their stringified representation when asked. Python certainly keeps track of their type. I know this because type(f) tells me so. So why not go that extra mile and do the conversion for me? If I was worried about nanoseconds of execution time, I wouldn't be using Python in the first place.


Even languages whose OOP nature is a lipstick-on-a-pig situation, like Perl or VB6 can field this with grace.


OK, sorry. I didn't mean for this to be a rant. What I really wanted was to ask for help.


Is there a library, maybe some user written code, maybe a wrapper function that will simply allow me to do something like this:



f = 5.0
g = (1, 2, 3)
h = [a, b, c]
print 'f: ' + ', ' + f + ', g: ' + g + ', h: ' + h


without having to do the conversion to string myself? I'm allowed to be lazy. I'm a programmer. It's my birthright...


Thanks!





Aucun commentaire:

Enregistrer un commentaire