- View the source.
- Go to Python/関数の定義.
3: 2016-08-17 (Wed) 12:07:37 njf ![]() ![]() |
Cur: 2017-01-02 (Mon) 17:04:47 njf ![]() ![]() |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | [[Python]]での関数の定義はdefで行う。 | + | 「[[Python/関数定義]]」を参照のこと。 |
- | + | ||
- | def testFunction(): | + | |
- | print "test!" | + | |
- | + | ||
- | 実行 | + | |
- | testFunction() | + | |
- | + | ||
- | 結果 | + | |
- | + | ||
- | test! | + | |
- | + | ||
- | 引数や戻り値ももちろん定義できる。 | + | |
- | + | ||
- | def test2Function(str): | + | |
- | return str | + | |
- | 実行 | + | |
- | print test2Function("test2!") | + | |
- | + | ||
- | 結果 | + | |
- | + | ||
- | test2! | + | |
- | + | ||
- | 引数のデフォルト値も定義できる。 | + | |
- | + | ||
- | def test3Function(a=1,b=2,c=3): | + | |
- | print "%d %d %d" % (a, b, c) | + | |
- | + | ||
- | >>> test3Function() | + | |
- | 1 2 3 | + | |
- | >>> test3Function(2) | + | |
- | 2 2 3 | + | |
- | >>> test3Function(2,3) | + | |
- | 2 3 3 | + | |
- | >>> test3Function(2,3,4) | + | |
- | 2 3 4 | + | |
- | + | ||
- | また、特定の引数のみを明示して実行もできる。 | + | |
- | >>> test3Function(b=2) | + | |
- | 1 2 3 | + | |
- | + | ||
- | 関数を変数にする事も可能。 | + | |
- | + | ||
- | >>> t = test3Function | + | |
- | >>> t() | + | |
- | 1 2 3 | + |
- Backup list of Python/関数の定義
- Backup diff of Python/関数の定義(No. All)
- 1: 2016-07-24 (Sun) 05:57:00 njf
- 2: 2016-07-24 (Sun) 06:05:06 njf
- 3: 2016-08-17 (Wed) 12:07:37 njf
- Cur: 2017-01-02 (Mon) 17:04:47 njf
Page Info | |
---|---|
Page Name : | Python/関数の定義 |
Page aliases : | None |
Page owner : | njf |
Can Read | |
Groups : | All visitors |
Users : | All visitors |
Can Edit | |
Groups : | All visitors |
Users : | All visitors |
Counter: 1574,
today: 1,
yesterday: 0