Learning to program - The Basics - The Raw Materials - Other Collection Types~

Other Collection Typesからの続き。

一応全部先に飛ばしたところを読んでみたけど難解だなぁ、こりゃあの時飛ばしておくべきだったわ。たぶんクリアできるかな。次はこれをクリアしてRaw Materialsの理解の完了、そしてModule & Functionの理解の終了、そしてさらに次のHandling Files、Handling Textに入っていきたい。Handling Filesは終わらしたいね。事前に読んであるから可能なはず。

One useful property of stacks is that you can reverse a list of items by pushing the list onto the stack then popping it off again. The result will be the reverse of the starting list.

Complex/User Defined

Sometimes the basic types described above are inadequate even when combined in collections. Sometimes, what we want to do is group several bits of data together then treat it as a single item. An example might be the description of an address:
a house number, a street and a town. Finally there's the post code or zip code.

Most languages allow us to group such information together in a record or structure or with the more modern, object oriented version, a class.

The main thing I want you to recognize in all of this is that we have gathered several pieces of data into a single structure.

We can assign a complex data type to a variable too, but to access the individual fields of the type we must use some special access mechanism (which will be defined by the language). Usually this is a dot.

Here we first of all Dimension a new variable, Addr, using Dim then we use the Set keyword to create a new instance of the Address class.

コード:VBScript

<html>
<body>
<script type="text/vbscript">
Class Address
     Public HsNumber
     Public Street
     Public Town
     Public ZipCode
End Class

Dim Addr
Set Addr = New Address

Addr.HsNumber = 7
Addr.Street = "High St"
Addr.Town = "Anytown"
Addr.ZipCode = "123 456"

MsgBox Addr.HsNumber & " " & Addr.Street & " " & Addr.Town

</script>

</body>
</html>

コード:Python

class Address:
    def __init__(self, Hs, St, Town, Zip):
        self.HsNumber = Hs
        self.Street = St
        self.Town = Town
        self.ZipCode = Zip

Addr = Address(7, "High St", "AnyTown", "123 456")
debianAddr = Address(100, "Debian St", "DebiTown", "0010101")
print Addr.HsNumber, Addr.Street, Addr.Town
print debianAddr.HsNumber, debianAddr.Street, debianAddr.ZipCode

debianAddr.HsNumber = 1010100
print debianAddr.HsNumber

ちょっとAddressオブジェクトいじってみた。

User defined types can, in some languages, have operations defined too. This is the basis of what is known as object oriented programming. We dedicate a whole section to this topic later but essentially an object is a collection of data elements and the operations associated with that data, wrapped up as a single unit. Python uses objects extensively in its standard library of modules and also allows us as programmers to create our own object types.

Object operations are accessed in the same way as data members of a user defined type, via the dot operator, but otherwise look like functions. These special functions are called methods. We have already seen this with the append() operation of a list. Recall that to use it we must tag the function call onto the variable name:


When an object type, known as a class, is provided in a Python module we must import the module (as we did with sys earlier), then prefix the object type with the module name when creating an instance that we can store in a variable (while still using the parentheses, of course). We can then use the variable without using the module name.

We will illustrate this by considering a fictitious module meat which provides a Spam class. We import the module, create an instance of Spam, assigning it the name mySpam and then use mySpam to access its operations and data like so:

>>> import meat
>>> mySpam = meat.Spam() # create an instance, use module name
>>> mySpam.slice() # use a Spam operation
>>> print mySpam.ingredients # access Spam data
{"Pork":"40%", "Ham":"45%", "Fat":"15%"}

ちょっとわかりにくかったが、ここ(スパム)もやっと意味がわかった。事前にクラスというかオブジェクトを指定しておいてそれを使えるから、事前にオブジェクトをmoduleの中で指定しておいて、それをimportして使うってことが可能ってことか。

In the second line we use the meat module to create an instance of the Spam class - by calling it as if it were a function! In the third line we access one of the Spam class's operations, slice(), treating the object (mySpam) as if it were a module and the operation were in the module. Finally we access some data from within the mySpam object using the same module like syntax.


Other than the need to create an instance, there's no real difference between using objects provided within modules and functions found within modules. Think of the object name simply as a label which keeps related functions and variables grouped together.

Another way to look at it is that objects represent real world things, to which we as programmers can do things. That view is where the original idea of objects in programs came from: writing computer simulations of real world situations.

なんかオブジェクトかっこいいな、現実のシミュレーションだなんだ、もう自分のやりたいことまんまじゃないか。

Both VBScript and JavaScript work with objects and in fact that's exactly what we have been using in each of the Address examples above. We have defined a class and then created an instance which we assigned to a variable so that we could access the instance's properties. Go back and review the previous sections in terms of what we've just said about classes and objects.

《コ》クラス◆プログラマーによって定義された、一定の振る舞いを持つオブジェクトの構造。

Think about how classes provide a mechanism for creating new types of data in our programs by binding together the data and operations of the new type.


There are many more, it's often said that "Python comes with the batteries included". For details of how most of these Python specific operations work you'll need to consult the Python documentation.

Python Documentation Index

よっしゃー、終わったよ。これも結構てこずったけど、読んだときに考えたよりは楽勝だったな。ただこれから使えるかというとまだまだだけどw

Pythonだとmoduleのファンクションを読み込む・呼び出すのと同じようにオブジェクトを呼び出すんだみたいな感じの文のところは結構重要なのかなと思ったり。その理解を深めるため(何故そうなっているのかとか、それがどういう意味をプログラミング時に持ってくるのかとか)には後で戻ってきたいところ。
あぁ、僕が次に学ぶことになる順序的にModules & Functionsが来るからそこで少しは理解に貢献するといいんだけど。

関係ないけど、段々ブログが綺麗に書けてきたと思うんだ。HTMLでのドキュメンテーションっつーのがちょっとわかってきたみたいな。これもプログラミングを学んでいるからだな(謎)