The scope of names defined in class block doesn't extend to the methods' blocks. Why is that?

前端 未结 2 795
温柔的废话
温柔的废话 2020-11-30 06:49

Reading the documentation I came across the following paragraph:

A scope defines the visibility of a name within a block. If a local variable is def

2条回答
  •  旧时难觅i
    2020-11-30 07:35

    This seems to be related to the use of an explicit self parameter, and the requirement that all method calls and instance attribute accesses explicitly use self. It would be at least strange if the uncommon case of accessing a class scope function as a normal function would be much easier than the common case of accessing it as a method via self. Class variables are usually also accessed via the instance in Python.

    In C++, in contrast, the class scope is visibile in all methods, but calling a method implicitly passes this. This seems to be the other sane choice.

提交回复
热议问题