class

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(Omi Chiba)

class

Pythonは動的型付けなので、Pythonのクラスとオブジェクトは少し変わったものに見えるかもしれません。実際、クラスを宣言する際にメンバ変数(属性)を必ずしも定義する必要はなく、同じクラスから作られた異なるインスタンスはそれぞれ違うメンバ変数(属性)を持つことができます。 一般的に属性は、クラスではなくインスタンスに関連付けられます(ただし、クラス属性として宣言された場合は別です。これはC++/Javaでの"静的メンバ変数と同じです)。

Because Python is dynamically typed, Python classes and objects may seem odd. In fact, you do not need to define the member variables (attributes) when declaring a class, and different instances of the same class can have different attributes. Attributes are generally associated with the instance, not the class (except when declared as "class attributes", which is the same as "static member variables" in C++/Java).

例を示します:

Here is an example:

1.

2.

3.

4.

5.

>>> class MyClass(object): pass

>>> myinstance = MyClass()

>>> myinstance.myvariable = 3

>>> print myinstance.myvariable

3

ここで、passは何もしないというコマンドであることに注意してください。この場合、何も含んでいないMyClassというクラスを定義するために用いられています。MyClass()はクラスのコンストラクタを呼び出し(この場合、デフォルトのコンストラクタ)、オブジェクト、つまり、このクラスのインスタンスを返します。クラス定義における(object)の部分は、このクラスが組み込みのobjectクラスを拡張したものであることを示しています。これは必須ではないですが、推奨される書き方です。

Notice that pass is a do-nothing command. In this case it is used to define a class MyClass that contains nothing. MyClass() calls the constructor of the class (in this case the default constructor) and returns an object, an instance of the class. The (object) in the class definition indicates that our class extends the built-in object class. This is not required, but it is good practice.

次により複雑なクラスの例を示します:

Here is a more complex class:

1.

2.

3.

4.

5.

6.

7.

8.

9.

>>> class MyClass(object):

>>> z = 2

>>> def __init__(self, a, b):

>>> self.x = a, self.y = b

>>> def add(self):

>>> return self.x + self.y + self.z

>>> myinstance = MyClass(3, 4)

>>> print myinstance.add()

9

クラスの内部で定義された関数はメソッドになります。 いくつかのメソッドは、特別な予約済みの名前を持ちます。たとえば、__init__はコンストラクタです。すべての変数は、メソッドの外で定義されたものでない限り、メソッドのローカル変数です。たとえば、zはクラス変数です。これは、C++の静的メンバ変数と同じで、そのクラスのすべてのインスタンスに対して同じ値を保持します。

Functions declared inside the class are methods. Some methods have special reserved names. For example, __init__ is the constructor. All variables are local variables of the method except variables declared outside methods. For example, z is a class variable, equivalent to a C++ static member variable that holds the same value for all instances of the class.

ここで、__init__は3つの引数をとり、addは1つの引数をとっているのに対し、それらはそれぞれ2つの引数と、0個の引数によって呼ばれている点に注意してください。 最初の引数は、慣例的に、メソッド内で利用されるローカルな名前を示していて、現在のオブジェクトを参照します。 ここでは、selfを現在のオブジェクトを参照するのに利用しています。ただし、任意の他の名前を用いることも可能です。selfはC++の*thisやJavaのthisと同じ役割を担います。ただし、selfは予約語ではありません。

Notice that __init__ takes 3 arguments and add takes one, and yet we call them with 2 and 0 arguments respectively. The first argument represents, by convention, the local name used inside the method to refer to the current object. Here we use self to refer to the current object, but we could have used any other name. self plays the same role as *this in C++ orthis in Java, but self is not a reserved keyword.

この構文は、ネストしたクラス、たとえばあるクラス内のメソッドにおいてローカルなクラス、を宣言するときに、曖昧さを避けるために必要です。

This syntax is necessary to avoid ambiguity when declaring nested classes, such as a class that is local to a method inside another class.