编程语言中的变量提升的优点和缺点是什么? [英] What are the benefits and drawbacks of variable hoisting in programming languages?

查看:325
本文介绍了编程语言中的变量提升的优点和缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下Python程序A如预期那样输出 1 ,而以下Python程序B引发未绑定的局部变量 x 错误,违反直觉。

The following Python program A outputs 1, as expected, while the following Python program B raises an unbound local variable x error, counterintuitively.


  • 程序A:

def f(): print(x)
x = 1
f()



  • 程序B:

  • def f(): print(x); x = 2
    x = 1
    f()
    

    JavaScript具有完全相同的行为。

    Javascript has the exact same behaviour.


    • 程序A:

    function f() { console.log(x); }
    let x = 1;
    f();
    



    • 程序B:

    • function f() { console.log(x); let x = 2; }
      let x = 1;
      f();
      

      但是,在两种情况下,C ++的输出均符合预期, 1

      However, C++ outputs 1 in both cases, as expected.


      • 程序A:

      #include <iostream>
      int x;
      void f() { std::cout << x; }
      int main() { x = 1; f(); return 0; }
      



      • 程序B:

      • #include <iostream>
        int x;
        void f() { std::cout << x; int x = 2; }
        int main() { x = 1; f(); return 0; }
        

        因此所有程序A的输出 1 。一方面,Python和Javascript之间的程序B差异,另一方面,与C ++的差异是由于它们的作用域规则造成的:在C ++中,变量的范围始于其声明,而在Python和Javascript中,它从声明该变量的块的开始开始。因此,在C ++中,函数 f 中的打印变量 x 解析为值 1 x 的c $ c>,因为它是当前执行时上下文中的唯一变量。在Python和Javascript中,函数 f 中的打印变量 x 解析为空并引发未绑定的局部变量 x 错误,因为本地变量 x 在执行时已存在于上下文中,因此它掩盖了全局变量 x ,但尚未绑定到值 2 。 Python和Javascript的这种违反直觉的行为也称为变量提升,因为它在块的开始处提升了变量声明(而不是定义)。

        So all programs A output 1. The differences in programs B between Python and Javascript on the one hand, and C++ on the other hand, result from their different scoping rules: in C++, the scope of a variable starts at its declaration, while in Python and Javascript, it starts at the beginning of the block where the variable is declared. Consequently, in C++ printing variable x in function f resolves to the value 1 of global variable x since it is the only variable in context at this point of execution. In Python and Javascript printing variable x in function f resolves to nothing and raises an unbound local variable x error since local variable x is already in context at this point of execution and therefore it masks global variable x without being bound yet to the value 2. This counterintuitive behaviour of Python and Javascript is also known as variable hoisting since it ‘hoists’ variable declarations (but not definitions) at the beginning of their blocks.

        使用编程语言进行变量提升的优点和缺点是什么?

        推荐答案

        这更多是语言的人工产物,而不是面向程序员的功能。

        This is more a artifact of the language, rather than a programmer oriented feature.

        对于python和javascript,新变量意味着在名称字典中分配一个条目,直到您实际创建一个对象并分配它。在C ++中,运行时没有名称字典,该定义实际上需要为该对象分配内存(对于我们所知道的,它可能是10MB数组)。

        For python and javascript, the new variable means allocate an entry in the name dictionary, until you actually create an object and assign it. In C++ there is no name dictionary in the run-time, the definition needs to actually allocate memory for the object (and it could be a 10MB array for all we know).

        如果确实需要,可以允许C ++占用较小的内存。否则,没有太多考虑的理由。

        This does allow C++ to fit into smaller memory footprint if you really need that. Otherwise there's no much reason to think about it.

        从开发人员的角度来看,您有一个错误。您的 x 有2种含义。编程是足够困难的,因为它没有变量对您的含义的影响,所以我会尽可能避免。我认为C ++在某些情况下会向您发出警告。

        From the developer's perspective you have a bug. Your x has 2 meanings. Programming is hard enough as it is without having variables change meaning on you, so I would avoid as much as possible. I think C++ would give you a warning in some such a cases.

        在实践中,您会习惯于任何一种设置。

        In practice you would get used to either setup.

        这篇关于编程语言中的变量提升的优点和缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆