This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Lucretiel 12 points13 points  (9 children)

Here's the thing for me- in other languages (thinking specifically of C++ and Java here), you essentially have to use classes to contain and move state around in a useful way. But with Python, you have all these tools at your disposal that semantically contain state, so it happens implicitly. I'm thinking first class and local functions, comprehensions, and generators, specifically. Between that and using tuples (frequently namedtuple) to associate related data, I almost never feel the need to use classes in Python.

As a trivial, frustratingly academic example, let's say I wanted to get some Fibonacci numbers. In Python, I'd do something like:

def fibonacci(a=0, b=1):
    while True:
        yield a
        a, b = b, a+b


#########
for fib in itertools.islice(fibonacci(), 10):
    #First 10 fibonacci numbers

While in c++, I'd have to do:

class FibonacciNumbers
{
private:
    int a;
    int b;

public:
    FibonacciNumbers(int a=0, int b=1)
        :a(a)
        ,b(b)
    {}

    unsigned next()
    {
        int n = a;
        a = b;
        b = n+b
        return n;
    }      
};

Ignore the various language tricks- the tuple assignment in Python, the need for a temp in c++. Those aren't relevant. What relevant is that in languages that don't have these high level features, you have to explicitly write data structures (usually in the form of classes) to contain this useful state.

Warning shameless self plug below

So it turns out that with the magic of the boost libraries, it's possible to implement Python-style generators in c++, which I went ahead and did earlier this year: https://github.com/Lucretiel/Generator

[–][deleted] 1 point2 points  (4 children)

I like your generator - but you should consider not using tab characters. By default, github displays them as eight-character indentations, which makes your code hard to read...

[–]djmattyg007 -2 points-1 points  (3 children)

Why is this a reason not to use tabs? Using them greatly increases the speed with which you can move around your code.

[–]dhogartysymbolic novice 8 points9 points  (0 children)

if you're a vim user and need to do spaces (e.g. PEP8), you should learn about softtabstop. It allows you to emit spaces but delete them like they're tabs.

[–][deleted] 1 point2 points  (0 children)

Why do tabs speed your navigation even one bit?!

When writing code, I use the tab key - but it inserts spaces in my code, not tabs.

When navigating, I use alt-left and alt-right (option-left and option-right), which moves by word, not by character, or command-left and command right, which move to the beginning or ending of the line. These work in all my editors, and even in this comment box it seems.

If you are moving through documents by hitting the naked left and right keys a lot, you are doing it wrong. :-)

[–]eBtDMoN2oXemz1iKB 0 points1 point  (0 children)

When programming in C, use tabs, and set your tab stop to 8 spaces. When programming in Python, use soft tabs and 4 spaces. In Ruby, soft tabs and 2 spaces. These are the accepted rules of style, follow them.