Categories
python ruby

Trick that work in irb and python shell

This irb tip is from Natasha the Robot. Often we forgot to assign an expression to a variable, you could of course use up arrow to retrieve last command and then move all the way to the front, add a variable and equal sign to it, but that is a lot of troubles.

An easy way is just assign a variable to underscore, like this

text = _

Then you have your previous expression assigned to a variable called text. Magical.

What is more surprising is that just found out that this magical trick works the same in python shell! Even more magical.

Categories
python

staticmethod in python

Going through byte of python 3, I tried to make sense of staticmethod in the section talking about class and Object variables.

In the Robot class, there is a howMany class method, declared as below

def howMany():
  print("we have {0:d} robots".format(Robot.population))
howMany = staticmethod(howMany)

or using decorator, it will be

@staticmethod
def howMany():
  print("we have {0:d} robots".format(Robot.population))
howMany = staticmethod(howMany)

It was then called by the Robot class,

Robot.howMany()

I couldn’t really understand what is the staticmethod role here though.
According to the Python documentation,

A static method does not receive an implicit first argument.
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

That explain a little bit more, seems a staticmethod can be called either by class or instance.
Then I try something like this,

class Human:
  def communicate():
    print("I talk")
  def move():
    print("I walk")
  move = staticmethod(move)

So a Human class have two class methods, communicate and move, but move is a staticmethod.

Steve = Human()
Steve.communicate()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: communicate() takes no arguments (1 given)
#communicate() is a class method, when object Steve try to call it, an error message is saying communicate take no arguments, but 1 is given. I guess that is object Steve trying to pass self to communicate()?
Steve.walk()
#It duly print out "I walk"

Seems to me, staticmethod is a way for object to access class method that otherwise wouldn’t be available to them. Is that the correct perception?

Categories
python

Clear screen in python interactive shell mode

Here is how to clear screen when you are working in the python interactive shell mode,

import os
os.system('cls')

A quick google actually yields a few solutions, I find this work best for me.

Categories
python

Exception handling in python

As I came across the exception handling chapter, both code below should work as stated in the book (The Head First Python book, which is indented for Python 3.X, but I am running this code in Python 2.X),

try:
  with open("man_data.txt") as man_file:
    print >>man_file, man
  with open("other_data") as other_file:
    print >>other_file, other

or

try:
  with open("man_data.txt") as man_file, open("other_data") as other_file:
    print >>man_file, man
    print >>other_file, other

However it seems only the first one is working under Python 2.X, later code will in fact throw up a syntax error message.
Didn’t see anything on the net mentioning this has to do with changes in Python 3.x, so not sure if that is the case.

Categories
python

Print without newline in Python

Going thru the Head First Python book, which is intended for Python 3.X, although majority of the content work just as well in Python 2.X.

First encountered with some variations between version 3.X and 2.X at the chapter about printing out nested lists, with tab indicating different nested level.

To print a tab without a newline, you could do this in Python 3.X,

print("\t",end='')

However, the same code will throw you a syntax error in Python 2.X.
So to do the same in Python 2.X,

print "\t",

Note the final comma, which actually make sure the line will print out with space instead of a newline.
In Python 3.X, print is an actual function but in Python 2.X, print is still a statement.

I found out this from the ever helpful stackoverflow