未分類

python3 でクラスの初期化を __init__ で行う

投稿日:

python でクラスのオブジェクトを生成したとき、__init__ メソッドが呼び出される(コンストラクタ)

__init__ の引数には自身(self)を指定することが通例。

一方、オブジェクト破棄の際に呼び出されるデストラクタは、__del__ と書かれる。

class Car:
 def __init__(self):
  self.name = "Audi"
 def sayname(self):
  print("My name is " + self.name)

car = Car()
car.sayname()

結果

My name is Audi

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

tex で脚注を表示する方法

\footnote を使う。 例 \footnote{this is a footnote.} 結果

no image

tex で行内を結合する方法

\multicolumn を使って、結合する行の成分の数を指定することができる。次の例では「3」を指定して、結合している。 例 \begin{table} \begin{tabular}{ |c|c| …

no image

mac の convert コマンドで画像ファイルをサイズ変更する

convert コマンドを使うには、imagemagick というソフトがインストールされていることが必要。 インストールの確認 $ which convert で、imagemagick がインスト …

no image

matplotlib で x, y軸・タイトルのフォントサイズを調整する

ラベルの fontsize で指定する。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2, 100) …

no image

tex の表で、複数行を1行内に入れる方法

multirow パッケージを使っておき、表内で multirow で入力する。 例 \usepackage{multirow} \begin{document} \begin{table} \beg …