4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python as a strongly, dynamically typed language

Posted at

講義を見ていたら先生がドヤ顔で「pythonはjavaと違ってdynamically-typedなんだ」と言われたので腹いせにググッてやったら全く新しい世界に巡り会えたので忘れないようにまとめておく。先生ありがとう、先生からしたらきっとkudo to those who googled the s*** out of itなんでしょう。。恐らくこれは触りだけだと思いますが。

はじめに

まず共通認識としてpythonはstrongly dynamically typedと称されることが一般的だそうです。このstrongly dynamically typedというのは__strong typing__と__dynamic typing__の二つの性質を併せ持った言語である、ということです。続いてこの2つの意味について触れていきます。主にvariable declaration(変数の定義の仕方について)と深く関係しているようです。また(他の言語と比べてpythonにはほとんど関係していないかもしれませんが)厳密には変数の定義などに関してはコンパイラが変数をどう認識しているのかetcが関わってくるらしいのですが、僕も正直そこまで理解していないのでここでは割愛しておきます。

以下の回答が個人的にイメージを掴む上で分かりやすく簡潔だったので載せておきます。

Python is strongly typed as the interpreter keeps track of all variables types. It's also very dynamic as it rarely uses what it knows to limit variable usage. In Python, it's the program's responsibility to use built-in functions like isinstance() and issubclass() to test variable types and correct usage. Python tries to stay out of your way while giving you all you need to implement strong type checking.

まとめてしまえばまさにこういうことです。ただ先生曰くpythonは自分で勝手にtypes of parameterを探してくれるのでコンパイラに伝える必要が無い。その代わりプログラムを実行するまで何を行ったのか(or 何がエラーを吐いた原因なのか)分からないという欠点があるそうです。

strongly typed?

In a strongly typed language, you are simply not allowed to do anything that's incompatible with the type of data you're working with. For example, in a weakly typed language you can typically do 3 + 5 + 7 and get the result 15, because numbers can be added; similarly, you can often do 'Hello' + 'And' + 'Goodbye' and get the result "HelloAndGoodBye", because strings support concatenation. But in a strongly-typed language you can't do 'Hello' + 5 + 'Goodbye', because there's no defined way to "add" strings and numbers to each other. In a weakly typed language, the compiler or interpreter can perform behind-the-scenes conversions to make these types of operations work; for example, a weakly typed language might give you the string "Hello5Goodbye" as a result for 'Hello' + 5 + 'Goodbye'. The advantage to a strongly typed language is that you can trust what's going on: if you do something wrong, your program will generate a type error telling you where you went wrong, and you don't have to memorize a lot of arcane type-conversion rules or try to debug a situation where your variables have been silently changed without your knowledge.

strong typingというのは砕けた言い方をするとvalueの種類が突然変わったりしないということを指すようです。逆の言い方をするとvalueの種類を換えるためには「はい、変えますよー」と宣言をしてあげないといけないのです。その宣言が(全てに適用するわけではないが)必要か否かっていうだけのことです。例えばpythonではストリング上の数字はいきなり数字になったりしませんし(逆にperlなど他の言語ではするらしい。。。)

とはいえ言語によっても差異があり、それはprimitives(+, -, /, * etc..)とlibrary functionsがそれぞれのオブジェクに対してどう反応するかによっても変わってくるようです。どういうことかというと「strongly typedという性質を持っている = weakly-typedという性質を持てない」というわけでもないそうです。

The strength of the type system in a dynamic language such as Python is really determined by how its primitives and library functions respond to different types. E.g., + is overloaded so that it works on two numbers or two strings, but not a string and an number. This is a design choice made when + was implemented, but not really a necessity following from the language's semantics. In fact, when you overload + on a custom type, you can make it implicitly convert anything to a number:

def to_number(x):
    """Try to convert x to a number."""
    if x is None:
        return 0
    # more special cases here
    else:
        return float(x)  # works for numbers and strings
>
class Foo(object):
    def __add__(self, other):
        other = to_number(other)
        # now do the addition

dynamically typed?

Python is strongly typed as the interpreter keeps track of all variables types. It's also very dynamic as it rarely uses what it knows to limit variable usage. In Python, it's the program's responsibility to use built-in functions like isinstance() and issubclass() to test variable types and correct usage. Python tries to stay out of your way while giving you all you need to implement strong type checking.

dynamic typingは変数自体ではなく、ランライムオブジェクト(いわゆるvalue)が種類を持っているのに対して、static typing(dynamic typingの逆)は変数自体がその種類を持っていることを意味します。
そんなことが何の役に立つの?って話ですが、例えばpythonでは:

x = 1
x = "x"

といったことが動作もなくできます。これはなぜかというとpythonでは変数はvalueの種類(type != typing)を持たないからです。1というvalueがintという種類を持って定義されているので後からその種類を変えることができます。またpythonではどんなオブジェクトでも定義付けすることができます。ちなみにtype()で確認できます。

参考にしたリンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?