2011年3月29日火曜日

[javascript]空配列[]の真偽値

配列が空であることを確認したい時、
var a=[];
if(a === []){...}
と書くと間違い。

参考に、下のサイトをちぇっく。
http://d.hatena.ne.jp/reosablo/20090519/1242723949

要するに、
[]は配列、配列はオブジェクト、だから基本的にtrue。
でもif([] ===...
のようにif文章の左辺で使われるときは、[配列]は[配列].toStringと見なされる。
(配列のtoStringメソッドはDavid FlanaganのJavaScript第5版121ページ参照。)

例えば、
var a=[];
if(a){} //true
if(a==true){} //false

var b=[1];
if(b==true){} //true
var c=[2];
if(c==true){} //false

のようになる。bのプロセスを説明すると、

var c=[1,2];
if(c==true){} //false

var d=[{'num':1};
if(d==true){} //false

となることに要注意。

2011年3月26日土曜日

The Social Network Clip



Sean Parker: You know why I started Napster?
[Mark shakes his head]
Sean Parker: The girl I loved in high school was with the co-captain of varsity lacrosse team and I wanted to take her from him. So I decided to come up with the next big thing.
Mark Zuckerberg: I didn't know that.
Sean Parker: Napster wasn't a failure. I changed the music industry for better and for always. It may not have been good business but it pissed a lot of people off. And isn't that what your facemash was about? They're scared of me pal and they're gonna be scared of you. What the VC's wanted to say "Good idea, kid. Grown ups will take it from here". But not this time. This is our time. This time you're gonna...you're gonna hand 'em a business card that says "I'm CEO Bitch". That's what I want for you.



Lawyer: Mr. Zuckerberg, do I have your full attention?
Mark Zuckerberg: No.
Lawyer: Do you think I deserve it?
Mark Zuckerberg: What?
Lawyer: Do you think I deserve your full attention?
Mark: I had to swear an oath before we began this deposition, and I don't want to perjure myself, so I have a legal obligation to say no.
Lawyer: Okay...no. You don't think I deserve your attention.
Mark Zuckerberg: I think if your clients want to sit on my shoulders and call themselves tall, they have the right to give it a try, but there's no requirement that I enjoy sitting here listening to people lie. You have part of my attention - you have the minimum amount. The rest of my attention is back at the offices of Facebook, where my colleagues and I are doing things that no one in this room, including and especially your clients, are intellectually or creatively capable of doing. Did I adequately answer your condescending question?


'Let the Hacking begin'
http://www.youtube.com/watch?v=odOzMz-fOOw&feature=related

'drink and hacking'
http://www.youtube.com/watch?v=vAf9mUnafcQ

2011年3月18日金曜日

[Python]os.path

__file__はモジュールがロードされたファイルのパス。

例えば、

print __file__
//C:\Python27\module1.py

続いて、よく出てくる、os.path.dirname(__file__)の意味。
これは、__file__の入ったディレクトリのパスを表示する。

os.path.join(path1,path2,..)はpathを結合する。
次の例を見れば一目瞭然。

import os
print __file__
//C:\Python27\module1.py
print os.path.dirname(__file__)
//C:\Python27

print os.path.join(os.path.dirname(__file__),'index.htm')
//C:\Python27\index.htm

[参考文献]
http://pyref.infogami.com/__file__
http://effbot.org/librarybook/os-path.htm

if __name__ == "__main__" の意味。

モジュールの実行のされ方は2つ。
1.それ自体が実行される。
2.importされて呼び出される。

1のケースでは、__name__には"__main__"が代入され、
2のケースでは、__name__にはモジュール名が代入される(test.pyなら"test"が代入される)

ゆえに
if __name__ == "__main__"
main()

の意味は、このモジュールがそれ自体で呼び出されたときは、main()を実行し、
importされた場合はmain()はスルーしますよという意味。

[参考文献]
http://www.ibiblio.org/g2swap/byteofpython/read/module-name.html
http://d.hatena.ne.jp/s-n-k/20080512/1210611374

2011年3月2日水曜日

Knowing when to use self and __init__ in python

When defining your class methods, you must explicitly list self as the first argument for each method, including __init__. When you call a method of an ancestor class from within your class, you must include the self argument. But when you call your class method from outside, you do not specify anything for the self argument; you skip it entirely, and Python automatically adds the instance reference for you. I am aware that this is confusing at first; it's not really inconsistent, but it may appear inconsistent because it relies on a distinction (between bound and unbound methods) that you don't know about yet.

Whew. I realize that's a lot to absorb, but you'll get the hang of it. All Python classes work the same way, so once you learn one, you've learned them all. If you forget everything else, remember this one thing, because I promise it will trip you up:


__init__ methods are optional, but when you define one, you must remember to explicitly call the ancestor's __init__ method (if it defines one). This is more generally true: whenever a descendant wants to extend the behavior of the ancestor, the descendant method must explicitly call the ancestor method at the proper time, with the proper arguments.

Dive into python