Entries from 2013-01-01 to 1 year

Useful method "random.choice"

In case of choosing an element in a list randomly, I usually write a code like below.Ruby: ary = [1, 2, 3, 4, 5] print ary[rand(0...ary.length)] But in python, an useful method exists. import random ary = [1, 2, 3, 4, 5] print(random.choic…

How to use os.walk in python

This is a way that show all file names under current directory. import os for dirpath, dirnames, filenames in os.walk("."): for filename in filenames: print(os.path.join(dirpath, filename))

The Idea of "class" in python

I have studied Python recently and I found little differences between python and other programming languages.class definition: class Person(SuperClass, SuperClass2): # multiple inheritance is possible i = 1 # class variable # constructor d…

大域脱出(ruby, java)

rubyには大域脱出という機能があることを思い出したのでメモ。 通常のbreakだと複数のループからは抜けられないが、大域脱出を使えば任意のラベルの箇所まで脱出が可能。goto文がない言語だと大域脱出 or 例外処理を使う必要がある。 以下rubyの例。 puts "=…

モジュール変数について

モジュールの内部でクラス変数のようなモジュール変数を定義できるらしい。 require "active_support/all" module Test mattr_accessor :var @@var = 1 end Test.var #=> 1 Test::var #=> 1 ちなみにmattr_accessorを使うにはrequire "active_support/all"し…