Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>main() in python is just a function name. The common idiom </p> <pre><code>if __name__ == '__main__': #do something </code></pre> <p>is a shortcut to figure out of the code in this file is being run as a program rather than imported as a module.</p> <p>Because python is intended to by type-free, the community puts a strong emphasis on conventions and best practices. This doesn't provide the same level of predictability as a compiler, but it helps keep the chaos at bay. Readability is a core python value and idioms like this provide valuable structure.</p> <p>Python does not support function overloading in the sense that other languages do. In strongly typed languages you might write multiple versions of the same function with the same name but varying input arguments:</p> <pre><code>void DoSomething (int a) {}; void DoSomething (float f) {}; void DoSomething (string s){}; </code></pre> <p>In python there is no equivalent idiom. In most cases it's unneeded: for a numeric operation you don't really care if the incoming numbers are floats, ints or whatever -- only that they support the correct operators. This is where the python mantra of 'duck typing' comes in - if it walks like a duck and quacks like a duck, it's a duck. So python functions idiomatically <a href="https://stackoverflow.com/questions/14435122/does-this-approach-to-python-duck-typing-mixed-with-isinstance-make-sense?rq=1">look for functions or operators on incoming arguments rather than checking their types</a>. </p> <p>As for instance vs static methods: In python every instance method implicitly gets the owning instance as the first argument to a function:</p> <pre><code>class Test(object): def __init__(self, arg): self.variable = arg def example(self): print self.variable fred = Test(999) # note: no 'self' passed here fred.example() &gt;&gt;&gt; 999 joe = Test(-1) joe.example() &gt;&gt;&gt; -1 </code></pre> <p>a class method gets the class type, rather than an instance, as the implicit first argument. Class methods can see class-level variables, which are defined in the class scope rather than in the instance scope - but they don't know anything about a particular instance.</p> <pre><code>class TestCls (Test) CLASS_VARIABLE = "hello" # other behavior inherited from Test @classmethod def class_example(cls): print cls.CLASS_VARIABLE barney = TestCls(123) barney.example() &gt;&gt;&gt; 123 barney.class_example() # again, no explicit class passed in &gt;&gt;&gt; 'hello' </code></pre> <p>a static method gets no implicit arguments at all:</p> <pre><code>class TestStatic (TestCls): CLASS_VARIABLE = 'goodbye' # inherited stuff again @staticmethod def static_test(): print "behold, I have no implicit argument" </code></pre> <p>Static and class methods also don't need an instance to be called:</p> <pre><code>wilma = TestStatic(123) wilma.static_test() # you can call from an instance &gt;&gt;&gt; behold, I have no implicit argument # or not: TestStatic.static_test() &gt;&gt;&gt; behold, I have no implicit argument TestStatic.class_example() &gt;&gt;&gt; goodbye # the method is inherited, but the class variable come from this class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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