サンプル集  >  Python  >  class
class
2023/05/20

classの利用例です。

◆環境
OS Windows 10 Home 22H2 64bit OS x64 プロセッサ
Python 3.9.6
VS Code 1.59.0

classTest.py
1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
class TestCalc(object):
    def myAdd(self, a, b):
        return a+b

cal = TestCalc()

x = cal.myAdd(3, 4)
print(x)

実行してみます。

PS C:\python> py .\classTest.py
7

期待通りに動作しました。

myAddの第一引数にselfを指定していないとエラーが出ました。

classTest.py
1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
class TestCalc(object):
    def myAdd(a, b):
        return a+b

cal = TestCalc()

x = cal.myAdd(3, 4)
print(x)

PS C:\python> py .\classTest.py
Traceback (most recent call last):
  File "C:\python\classTest.py", line 7, in <module>
    x = cal.myAdd(3, 4)
TypeError: myAdd() takes 2 positional arguments but 3 were given

見た感じの引数の数は合っていますが、3 were givenというエラーがでました。

▲ PageTop  ■ Home


Copyright (C) 2023 ymlib.com