Deeper Understanding Via Stackoverflow And Summarized By Pyankit

I fully resonate with Pyankit’s assertion that Stackoverflow is a golden mine but also rife with too much subpar information that a cherry picking by savvy users is valuable.  

  1. Using this try block is not recommendable because you want to catch the exception that you expect, passing it without doing anything about it is just a sign of presumption and laziness. (by poke)
try:
    something
except:
    pass
instead, use
def askForNumber ():
    while True:
        try:
            return int(input('Please enter a number: '))
        except ValueError:
            pass

2.  Why decorators and how decorator and decorator chain work?

The first why question is especially intriguing for me because I don’t want to spend time to grasp a concept or tool without actual usage of it. Learning is for using. In this thread,   the details are laid out.  Python itself provides several decorators: propertystaticmethod, etc.

3. Self in Python

Python is all for making things explicit, obvious, that’s why in Python, there is this odd expression of self in class objects. Python does not use the @ syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically, but not received automatically: the first parameter of methods is the instance the method is called on. That makes methods entirely the same as functions, and leaves the actual name to use up to you (although self is the convention, and people will generally frown at you when you use something else.) self is not special to the code, it’s just another object.

4.  Mutable unmutable in Python. Let’s look at this example

>>> def a():
… print(“a executed”)
… return []

>>>
>>> def b(x=a()):
… x.append(5)
… print(x)

a executed
>>> b()
[5]
>>> b()
[5, 5]

after typing b(), incremental list of 5 spits out

If we id(b()), even the list of b() keeps going up, the id of this function or object stays the same. effbot has a best explanation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.