LoginSignup
2
0

More than 5 years have passed since last update.

pythonをストレスなく使う!(Pythonでは、すべてがオブジェクトとして実装されている)

Last updated at Posted at 2019-05-26

目的

 pythonをストレスなく使う!
そのためには、少しでも、理解のレベルを上げる必要あり。
なんでも、こだわって、、、、理解を深める。

Pythonでは、すべてがオブジェクトとして実装されている

このタイトルを体現する。

その前に、念のため、書籍引用

  (出典:「入門 Python3」オライリー・ジャパン)

 Pythonでは、すべて(ブール値、整数、浮動小数点、文字列、もっと大きなデータ構造、関数、プログラム)がオブジェクトとして実装されている。

  (出典:「introducing Python」 O'Reilly Media,Inc)

 In Python, everything--booleans, integers, floats, strings, even large data structures, functions, and programs--is implemented as an object.

dir関数は、使えるメソッドが確認できる。
オブジェクトなら、、、、'Cat'や7や7.7もオブジェクトでしょう。
オブジェクトなら、メソッドもってるでしょう。
何ができるかdirで確認して、、、、

さーーーーっ!

>>> dir('Cat')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> 'Cat'.swapcase()
'cAT'
>>>
>>>
>>> dir(7.7)
['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__set_format__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
>>> 7.7.real
7.7
>>> 7.7.is_integer()
False
>>>
>>>
>>> dir(7)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
>>> 7.real
  File "<stdin>", line 1
    7.real
         ^
SyntaxError: invalid syntax
>>>

'Cat'も7.7も、以下の抜粋のとおり、オブジェクトらしい動きをしている。
満足です!
7は、SyntaxErrorだけど。。。。これは、大人の事情でしょう(笑 ★↓)
※ご指摘頂きました。「7.」が小数として扱われるとのこと。そうだと、SyntaxErrorですね。Pythonが正しい。→→空白や()で回避できます(と教えて頂きました。)

>>> 'Cat'.swapcase()
'cAT'
>>> 7.7.is_integer()
False

まとめ

『Pythonでは、すべてがオブジェクトとして実装されている』が、体感できた。

関連(本人)

英語と日本語、両方使ってPythonを丁寧に学ぶ。

今後

Python、学ぶぞーーーー。
コメントなどあれば、お願いします。:candy:

2
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0