Ramblings

March 12, 2008

Python one liner

Filed under: dev, python — michaelangela @ 2:47 pm

This is a nice one liner, and it just happened to be there, totally unrelated to the article. Well it’s used but it’s not the point. But still it’s nice.

Metaclass Programming in Python, Part 2: — Understanding the Arcana of Inheritance and Instance Creation —

for s in “Power Wealth Beauty”.split(): exec ‘%s=”%s”‘%(s,s)

The result is:
In [15]: for s in “Power Wealth Beauty”.split(): exec ‘%s=”%s”‘%(s,s)
….:
In [16]: Power

Out[16]: ‘Power’
In [17]: Wealth

Out[17]: ‘Wealth’
In [18]: Beauty

Out[18]: ‘Beauty’

So it’s a quick way to take some text and assign it to a variable with a value of itself.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: