top | item 7560911

(no title)

rodrodrod | 12 years ago

Neat! I'd be interested in seeing pythonic solutions written in other languages (to the extent that those languages may allow 'Pythonic' style), too. I find it fascinating to see how certain languages will, for one reason or another, trend towards certain design patterns and styles.

discuss

order

hcarvalhoalves|12 years ago

Actually, most Python I write (influence from reading experienced programmers) tends to the last one, although it's semi-jokingly:

    def fizzbuzz(n):
        return 'FizzBuzz' if n % 3 == 0 and n % 5 == 0 else None

    def fizz(n):
        return 'Fizz' if n % 3 == 0 else None

    def buzz(n):
        return 'Buzz' if n % 5 == 0 else None

    def fizz_andor_maybenot_buzz(n):
        print fizzbuzz(n) or fizz(n) or buzz(n) or str(n)

    map(fizz_andor_maybenot_buzz, xrange(1, 101))

It's pleasing to use HFOs in Python, as long as you don't abuse lambdas. Also, some functional types like `defaultdict` can be used to describe code/business logic with datastructures rather than a bunch of if's, keeping things tidy.

gamegoblin|12 years ago

I use defaultdict all the time. Very often when describing graphs.

  g = defaultdict(dict)
Allows you to do

  g[node1][node2] = edge_weight
without checking if node1 exists, and if not, saying g[node1] = {}

Also a neat trick (of dubious use) is:

  def auto_tree(): return defaultdict(auto_tree)
Gives you infinitely nested defaultdicts.

marcosdumay|12 years ago

I'm trying very hard to write Python in VB.Net at my job. But I've not been very successful, Python is very flexible.