ã¡ã½ããã®self
æè¿ãåãã¦ã®Rubyããèªãã§ãããPythonã®ã¡ã½ããã®ç¬¬ä¸å¼æ°ã«selfãæ¸ããªãã¦ã¯ãããªãããRubyã¨å¯¾æ¯ãã¦çç±ããªãã¨ãªãåãã£ãã®ã§æ¤è¨ãã¦ã¿ãã
ã¾ãPythonã§ã¯Rubyã¨åæ§ãã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¨ãããã¬ãã«é¢æ°ã®åºå¥ããªããPythonã§ã¯ã¡ã½ããã¨é¢æ°ã¨ããå¼ã³æ¹ããããé¢æ°ããã³ã¡ã½ããã¯åãªããªãã¸ã§ã¯ãã®å±æ§ã§ãããã¤ã¾ãã¡ã½ããã¯ã¯ã©ã¹ãªãã¸ã§ã¯ãã®callableãªå±æ§ãé¢æ°ã¯ã¢ã¸ã¥ã¼ã«ãªãã¸ã§ã¯ãã®callableãªå±æ§ã§ãããPythonã§ãå ¨ã¦ããªãã¸ã§ã¯ããªã®ã§ããã®åºå¥ãããªãã¨ãããã¨ãéè¦ã¨ãªãã
ããã§ã以ä¸ã®ã¯ã©ã¹å®ç¾©ãèãã¦ã¿ãã
>>> class Foo: ... a = 1 ... def foo(self): ... a # ã¨ã©ã¼ (1) ... Foo.a # OK (2) ... self.a # OK (3) ...
ã¯ã©ã¹å ã«é¢æ°ãå®ç¾©ããå ´åããã¼ã«ã«å¤æ°(ã°ãã¼ãã«å¤æ°)ãã¯ã©ã¹å¤æ°ãã¤ã³ã¹ã¿ã³ã¹å¤æ°ã®åºå¥ãèããªãã¦ã¯ãªããªããRubyã§ã¯å¤æ°ã®é ã«ãä½ãä»ããªãããã@ãä»ããããã@@ãä»ãããã§æ§æä¸åºå¥ãã¦ãããPythonã§ã¯ä»¥ä¸ã®ããã«ãªãã
1. ã¯ã©ã¹å ã§é¢æ°ãå®ç¾©ããã¨å±æ§ã«ãªãã®ã§ãã¯ã©ã¹å ã§å¤æ°ãå®ç¾©ãã¦ãå±æ§ã¨ãã
-
- å±æ§ãªã®ã§ãFoo.a ã®ãããªæ§æã§åç §ããã
2. 次ã«ãã¼ã«ã«å¤æ°(ã°ãã¼ãã«å¤æ°)ã¨ãã以å¤ã®ã¯ã©ã¹å¤æ°ãã¤ã³ã¹ã¿ã³ã¹å¤æ°ãåºå¥ãããã以ä¸ã®ããã«ãã
-
- ã¯ã©ã¹å¤æ°ã¯ã(2)ã®ããã«ã¯ã©ã¹åãå é ã«ä»ãã (ããã§ãå±æ§ã§ãã Foo.a ãã¯ã©ã¹å¤æ°ã¨èãããã¨ãã§ãã)
- ãã¼ã«ã«å¤æ°(ã°ãã¼ãã«å¤æ°)ã¯å é ã«ä½ãä»ããªã (ããã¯ãä»ã¨ä¸è²«æ§ãããå½ç¶ã§ãã)
- ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã¯ã(3)ã®ããã«selfãå é ã«ä»ãã (ããã§ãã¯ã©ã¹å¤æ°ã¨ãã¼ã«ã«å¤æ°ä»¥å¤ã®æ§æãç¨ããåç §æ¹æ³ãå¿ è¦ã¨ãªãã®ã§selfãå°å ¥ãã)
3. selfãã©ãã«ãããªãã¨ãããªã
-
- ããããPythonã§ã¯ã¡ã½ããã¨é¢æ°ã®åºå¥ããªããã¡ã½ãããå®ç¾©ãã ãç¹æ®ã«ããããªã
- ããã§ãã¡ã½ãã(ã¨ãªãã¹ãé¢æ°)ã§ã¯ã第ä¸å¼æ°ã«èªåèªèº«ã®ãªãã¸ã§ã¯ãã表ãselfã¨ããå¼æ°ãä»ãããã¨ãã決ã¾ããä½ã (selfã¨ããååã§ãªãã¦ãè¯ã)
- ããã¨ãå®ç¾©ããã¨ãã«ã¡ã½ããã¨é¢æ°ã®åºå¥ãããå¿ è¦ããªã
- ããããã¡ã½ããå¼ã³åºãã®åº¦ã«ç¬¬ä¸å¼æ°ã«ã¤ã³ã¹ã¿ã³ã¹ã渡ãã®ã¯é¢åãªã®ã§ãã¤ã³ã¹ã¿ã³ã¹ããã¯ã©ã¹å ã®é¢æ°ãåç §ããå ´åã第ä¸å¼æ°ã«èªåçã«ã¤ã³ã¹ã¿ã³ã¹ãªãã¸ã§ã¯ãã渡ããã¦ã«ãªã¼åãããã¡ã½ããã«èªåçã«ç½®ãæããã¨ããä»çµã¿ã«ãªã£ã¦ãã
ããã§ããã¤ã³ãã¨ãã¦ã¯Rubyã®ããã«selfã¯çç¥å¯ã¨ããå ´åãã¤ã³ã¹ã¿ã³ã¹å¤æ°ã«ç¹æ®ãªæ§æãç¨æããªãéãããã¼ã«ã«å¤æ°ã¨ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã®åºå¥ãã¤ããªããªã£ã¦ãã¾ããæ§æä¸åºå¥ããªãã¦ãè¯ãããlookupã®è¦åãè¤éã«ãªãé
ããªããã¾ãåãå¤æ°åã使ç¨ã§ããªãã¨ããå¶éãä»ãã¦ãã¾ãããªã®ã§é常ã¯éããã®ã§ããã¨ãã¦åºå¥ãããã®ã§ãselfãå°å
¥ãããããªãã
4. Rubyã®ãããªæé»ã®selfã¯ã©ãã ã£ãã®ãï¼
-
- ããã¯OKã ã£ããããããªããåç §ããã¨ã㯠self.aãå®ç¾©æã«ç¬¬ä¸å¼æ°ã¨ãã¦ã¯æ¸¡ããªã
- ãããããããã¬ãã«é¢æ°ã¯selfãã¢ã¸ã¥ã¼ã«ãªãã¸ã§ã¯ãã¨ãªãããã¯ã©ã¹ã¨ã¢ã¸ã¥ã¼ã«ã§ã¯å±æ§ã¨å¤æ°ã®æ±ããè¥å¹²ç°ãªã
- ã¾ããPythonã®æé»ããæ示ã¨ããããªã·ã¼ããä»ã®çµè«ã«éããã®ãããããªã
ã使ã
ä¸è¦§è¡¨ãè¦ã¤ãããªãã£ãã®ã§ã¾ã¨ãã¦ã¿ãããææ大æè¿ã
Python | Pythonista |
---|---|
Ruby | Rubyist |
Perl | Perl Monger |
PHP | PHPer (ãºã¡ã±ã¼) |
Scheme | Schemer |
Lisp | Lisper |
ML | MLer |
Java | Javanese |
C# | C#er (C Sharper) |
C | Cå±ãCãããã¼ |
C++ | C++å±ãCã·ãã·ãã¼ |
Delphi | Delphian |
Smalltalk | Smalltalker |
OCaml | Caml rider |
Haskell | Haskeller |
Scala | ? |
Erlang | ? |
Prolog | ? |
JavaScript | JavaScripter |
Brainfuck | ? |
JavaãC#ãC++ãªã©ãå¼ã³åãããã¹ããå¼ã³åããªãã®ã¯è¨èªãæããã¦ããªã証æ ï¼
OSãã®ä»ã追å ãã¦ã¿ããä»ã®å¼ã³æ¹ãããããããããã ããèªåã®ã¡ã¢ä»£ããã«ä¸è¬çã£ã½ãã®ãæ¸ãã¦ã¿ãã
Mac | ãã«ã¼ |
---|---|
Windows | ã㶠|
Linux | ãªãã¼ |
Tex | TeXãã·ã£ã³(ãããã·ã£ã³) |
ã¡ã¢
ãã¨ãã©ãã®éè¨å¸ iæ¦å£«ç¯ãæ¥è¨çµç±ã§ã¡ã¢ããã¨ã§èªãã
- Haskell ããã°ã©ãã³ã° ã ç´ç²é¢æ°åè¨èªã¸ã®èªãã(http://www.mew.org/~kazu/haskell.pdf)
以ä¸ãPythonçã®å®è£
ãåºå
¸å
ã¯çç¥ã
def qsort(L): if len(L) <= 1: return L return qsort([lt for lt in L[1:] if lt < L[0]]) + L[0:1] + \ qsort([ge for ge in L[1:] if ge >= L[0]])
- ã¨ã©ãã¹ããã¹ã®ãµãã
from itertools import ifilter, count def sieve(): g = count(2) while True: prime = g.next() yield prime g = ifilter(lambda x, prime=prime: x%prime, g)
Pythonãããç¡éãªã¹ãã®åãæ±ãããã£ã¨ä¸è¬åãã¦ãã ããã