Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Whatever you do, do NOT, I repeat, do NOT use Hungarian Notation! It causes severe brain &amp; bit rot.</p> <p>So, what can you do? Python and C/C++ are quite different. In C++ you typically handle polymorphic calls like so:</p> <pre><code>void doWithFooThing(FooThing *foo) { foo-&gt;bar(); } </code></pre> <p>Dynamic polymorphism in C++ depends on inheritance: the pointer passed to doWithFooThing may point only to instances of FooThing or one of its subclasses. Not so in Python:</p> <pre><code>def do_with_fooish(fooish): fooish.bar() </code></pre> <p>Here, any sufficiently fooish thing (i.e. everything that has a callable bar attribute) can be used, no matter how it is releated to any other fooish thing through inheritance.</p> <p>The point here is, in C++ you know what (base-)type every object has, whereas in Python you don't, and you don't care. What you try to achieve in Python is code that is reusable in as many situations as possible without having to force everthing under the rigid rule of class inheritance. Your naming should also reflect that. You dont write:</p> <pre><code>def some_action(a_list): ... </code></pre> <p>but:</p> <pre><code>def some_action(seq): ... </code></pre> <p>where seq might be not only a list, but any iterable sequence, be it list, tuple, dict, set, iterator, whatever.</p> <p>In general, you put emphasis on the intent of your code, instead of its the type structure. Instead of writing:</p> <pre><code>dict_of_strings_to_dates = {} </code></pre> <p>you write:</p> <pre><code>users_birthdays = {} </code></pre> <p>It also helps to keep functions short, even more so than in C/C++. Then you'll be easily able to see what's going on.</p> <p>Another thing: you shouldn't think of Python variables as pointers to memory. They're in fact dicionary entries:</p> <pre><code>assert foo.bar == getattr(foo, 'bar') == foo.__dict__['bar'] </code></pre> <p>Not always <em>exactly</em> so, I concur, but the details can be looked up at <a href="http://docs.python.org/reference/executionmodel.html#naming-and-binding" rel="nofollow">docs.python.org</a>.</p> <p>And, BTW, in Python you don't <em>declare</em> stuff like you do in C/C++. You just <em>define</em> stuff.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload