3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonのあんまり知らなそうな組み込み関数、変数、メソッド集 #5 [license, help, oct]

3
Posted at

前回の#4はこちら

license

前回にあった_Printerシリーズですね。

Type license() to see the full license text

printするとこのメッセージが出てきて、license()とすると
長いライセンステキストが出てきます。

help

これも_Printerと思いきや_Helperです。
なんとオブジェクトを引数に渡すとそのオブジェクトのヘルプが表示されます。
引数に何も渡さないと対話型ヘルプになります。

from typing import Literal

help(Literal)

こういうコードを書くと、

Help on _LiteralSpecialForm in module typing:

Literal = typing.Literal
Special typing form to define literal types (a.k.a. value types).

This form can be used to indicate to type checkers that the corresponding
variable or function parameter has a value equivalent to the provided
literal (or one of several literals)::

def validate_simple(data: Any) -> Literal[True]:  # always returns True
    ...
   
MODE = Literal['r', 'rb', 'w', 'wb']
def open_helper(file: str, mode: MODE) -> str:
    ...
   
open_helper('/some/path', 'r')  # Passes type check
open_helper('/other/path', 'typo')  # Error in type checker

Literal[...] cannot be subclassed. At runtime, an arbitrary value
is allowed as type argument to Literal[...], but type checkers may
impose restrictions.

と言ったふうにヘルプが表示されます。

oct

intを8進数に変換します。
あまり8進数を使う機会がなかったので初めて知ったのですが、
0oというプリフィクスのついた文字列が返ってくるようです。

print(oct(342391)) # 0o1234567

最後まで読んでいただきありがとうございます!

3
1
0

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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?