Learning to program - The Basics - Modules & Functions

how we use the many, many functions that come with any programming language (often called the library).

なるほど、ライブラリと呼ぶのか。プログラミング界隈で結構頻繁に聞く単語だよね。

Note: almost everything in JavaScript is an example of a special type of function called a method. A method is a function that is associated with an object (as discussed in the Raw Materials topic and in more detail later).

おいこれやってねーぞと思ってリンク先参照したら案の定飛ばしたところだった。くそ、あの複雑な章に戻らなきゃ完璧な理解は得られないのか。どないせよ。

とりあえず戻って読んでみるか。若干スピードダウンするが、まぁ、このまま突き進むよりも堅実だろう。それに今日は確実に進めてるし、それで問題なし。

というわけで、俺はthe Raw Materialsの後半を理解して実行して戻ってきたぞおおお、ジョジョー!

Note: almost everything in JavaScript is an example of a special type of function called a method. A method is a function that is associated with an object (as discussed in the Raw Materials topic and in more detail later). The main thing to note here is that the function is "attached" to the string s by the dot operator which means that s is the string that we will be performing the substitution upon.

This is nothing new. We have been using the write() method of the document object to display the output from our JavaScript programs (using document.write()) since the beginning of the tutorial, I just haven't explained the reason behind the dual name format up until now.

戻ってきてわかった。ここの言っている意味が、より深く。もろに存在しないmeatモジュールだかとその中に記述されているSpamファンクションと同じことをやっているわけですな。Javascriptの場合はそれがおそらく、Documentオブジェクトのwriteメソッドなわけだ。それを読み込んで・呼び出してきているというわけですな。なるほ。
少しずつ賢くなっている気がするぜ。やっと実感を感じないでもない。

Another useful function built in to python is dir which, when passed the name of a module displays all of the exported names within the module - including all of the variables and functions that you can use. Python comes with lots of modules, although we haven't really discussed them up till now. The dir function gives back a list of valid names - often functions - in that module.

このリストにさっき使ったpowというファンクションがあるぜよ。

from sys import * # import all names in sys
print path # can use without specifying prefix 'sys'
exit()

The big danger with this approach is that two modules could define functions with the same name and then we could only use the second one that we import (because it will override the first). If we only want to use a couple of items then it's safer to do it this way:

from sys import path, exit # import the ones we need
exit() # use without specifying prefix 'sys'


Note that the names we specify do not have the parentheses following them. If that was the case we would attempt to execute the functions rather than import them. The name of the function is all that is given.

この文の意味わからんなぁ。まぁインポートするときには()必要ないってことだと思うが。

import SimpleXMLRPCServer as s
s.SimpleXMLRPCRequestHandler()

なんかこれインタプリタで実行できんなぁ。まぁいいか。

Other Python modules and what they contain
少し試してみた。time, sleepとかは()で使える。randintとかは(1, 1000)とかで使える。ちょっと楽しい。()で使えないのはargument(引数)の数が足りませんよとか言われる。randintがそうだった。

There are literally dozens of modules provided with Python, and as many again that you can download. (A couple of good sources are the Vaults of Parnassus and, especially for more recent things, the Cheese shop.)

上の方はリンク死んでるくさいなぁ。後でぐぐってみればわかるかな。Cheese shopの方はPython関係調べてるとちょくちょく耳にするけど膨大な数だなぁ。

お、遂にFunction(関数)とProcedure(手続き)登場。

It's very nearly identical to the Sub syntax, however notice that you must assign the result to the function name inside the definition. The function returns as a result whatever value the function name contains when it exits:


Although we haven't followed this advice throughout this tutorial, it is usually best to avoid putting print statements inside functions. Instead, get them to return the result and print that from outside the function. That makes your functions much more reusable, in a wider variety of situations.

default Valueのあたりちょっとわからんなぁ。

def dayOfWeek(DayNum = None):

特にこの辺が、それにしては

print "The third day is %s" % dayOfWeek(2)

これで値渡してるし。そうすると初回に入れたNoneはどうなるんだと。これが何もarg(引数)を与えなかった場合に使われるデフォルトバリューってことなのか?おそらくそうなんだろうな。=で指定してるってこと自体が引数を初回に渡すってことと反しているし、関数を呼び出すときに基本的に引数の値を与えるという関数の構造を考えれば特長的だ。それがdefault valueの指定ってことなんだろう。

Note: We only need to use the time module if the default parameter value is involved, therefore we could defer the import operation until we need it. This would provide a slight performance improvement if we never had to use the default value feature of the function, but it is so small, and breaks the convention of importing at the top, that the gain isn't worth the extra confusion.

これも大体の理解としてはimport timeをif文の後ろにつけてもいいけど、それはimportをトップにつける慣習に反するし、混乱を招くので少しだけ得られるパフォーマンスの向上もそれに値しないからやめとけってことかな。

One way to get round this limitation of not changing built in behavior but still using a meaningful name for our functions is to put the functions inside either an object or a module which provides its own local context. We'll look at the object approach in the OOP topic a little later but for now let's see how we go about creating our own modules.

ここもちょっと読み取れんけど、まぁ、後でやるらしいのでいいだろう。OOPってのはObject Oriented Programingの略だと思うが。

Visual Basicは別にやらなくていいかなぁ。というか、あんまりやりたくない。MSの領域だと?と気が進まないwやったらやったで役に何かのときに理解とかそういうので立つんだろうが、困ったときか気が進んだときでいいだろう。

WSHの方をコピペしてみたんだが、動かないwwwちょwっをまwwwエラー出るwww
まぁソースの内容はわかるからいいかということにして、少しずつWSHも学んでいこうと思ったのである。次の章で使うから困りそうだけどw
とりあえずこの章終わり。

第1回 WSHを始めよう
一応ここをAutopagerizeで見ていって出てくるはじめの恥ずかしいHello World的な文はコマンドライン上で出力できるんだけどなぁ。