为什么要改变不可变的东西,如果它是全球性的 [英] Why you can change immutable if it's global

查看:48
本文介绍了为什么要改变不可变的东西,如果它是全球性的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么如果可变变量在Python中是全局变量,为什么我可以更改它?我不明白为什么在全球范围内不允许这样做,而在其他任何地方都不允许这样做.我最近开始学习编程,但我一无所获.下面的示例.

Can anyone explain to me why I can change immutable variable if it's global in Python? I don't understand why is this allowed in global state and not allowed everywhere else. I recently started learning programming and I just don't get it. Example below.

class Ddata():
    def __init__(self, iw, ih, fw, fh):
        self.iw = iw
        self.ih = ih
        self.fw = fw
        self.fh = fh

x = Ddata(1784, 366, 160, 180)

s_h = 0
s_w = 0
c_f = 20

def sp(x, cf, sw, sh):
    sw = x.fw * cf
    print sw
    if sw > x.iw:
        sw -= x.iw
        print sw
        sh = (sh + x.fh) + 4

sp(x, c_f, s_w, s_h)

print s_w
print s_h

print "----"  

def sp_global():
    global s_w
    global s_h
    global c_f
    global x
    s_w = x.fw * c_f
    print s_w
    if s_w > x.iw:
        s_w -= x.iw
        print s_w
        s_h = (s_h + x.fh) + 4

sp_global()

print s_w
print s_h

推荐答案

一些核心内容:

  • 根据变量的名称来命名变量,其他人都无法读取您的代码
  • 请勿使用 global .它从来没有必要,根本不是很好的做法,这将使您很难查看代码.我知道您是为了显示事物而这样做的,但仍然只是提示;)

  • Name your variables according to what they do, nobody can read your code else
  • Don't use global. Its never necessary, simply not good practice and will give you a hard time to overview your code. I know you did it to show things, but still, just the hint ;)

而且,如果还不清楚:实例变量(对象/类的变量)不是不可变的;)

And in case this is not clear: Instance Variables (vars of objects/classes) are NOT immutable ;)

关于不可变对象的事情:

Concerning the immutable objects thing:

不变性的概念并不是您在这里看到的.不可变变量表示创建后无法更改的变量.元组就是一个例子,因为您以后不能再更改它们的成分.

The concept of immutability is not what you are seeing here. Immutable variables mean variables you cant change after they are created. An example are tuples as you cant change their components afterwards.

您在这里看到的只是名称范围.

What you see here are simply name scopes.

如果在函数内更改变量,它将在其自己的本地名称范围内对其进行更改.尽管它们不会更改提供给他们的内容,但这会带来巨大的问题(考虑为函数指定的名称之类的变量,如果该名称会给您首个字母,如果该函数会自动更改名称,则程序会忘记",之后只有第一个字母).如果要在函数之外使用函数的结果,请返回它们.如果要使用类的概念,其中函数(对于类称为方法)会更改(类的)对象的属性(变量),则必须将这些函数声明为类的方法.在您的示例中,您将这样称呼它们: x.sp(...).

If you change a variable inside a function it changes them in their own local name scope. Though they do not change what is given to them as this would make huge problems (consider a variable like a name given to a function that should give you the first letter if the name. If the function would automatically change the name the program would "forget" it and only have the first letter afterwards). If you want to use the results of a function outside the function return them. If you want to use the concept of classes, where functions (for classes they are called methods) change attributes (variables) of the object (of a class) then you have to declare those functions as methods of the class. In your example you would then call them like this: x.sp(...).

有关类(面向对象程序设计)概念的更多信息,请在网上搜索,这里有很多不错的解释和示例.它是编程的核心概念,对学习非常有用!

For more on the concept of Classes (Object Oriented Programming) search the web there are plenty good explanations and examples. Its a core concept of programming and very useful to learn!

由于似乎不清楚,因此我为您的代码编辑了两种可能性:

As this didnt seem to be clear I edited your code for two possibilities:

class Ddata():
    def __init__(self, iw, ih, fw, fh):
        self.iw = iw
        self.ih = ih
        self.fw = fw
        self.fh = fh

        self.sh = 0
        self.sw = 0

    def sp(self, cf, sw, sh):
        sw = self.fw * cf
        print (sw)
        if sw > self.iw:
            sw -= self.iw
            print (sw)
            sh = (sh + self.fh) + 4

        self.sh = sh
        self.sw = sw

        return (self.sw, self.sh)



x = Ddata(1784, 366, 160, 180)

s_h = 0
s_w = 0
c_f = 20

result_of_sp = x.sp(c_f, s_w, s_h) #result is a tuple where we store two values


print (result_of_sp[0])
print (result_of_sp[1])

print ("----")  

x = Ddata(1784, 366, 160, 180)

def sp_global():
    global s_w
    global s_h
    global c_f
    global x
    s_w = x.fw * c_f
    print (s_w)
    if s_w > x.iw:
        s_w -= x.iw
        print (s_w)
        s_h = (s_h + x.fh) + 4

sp_global()

print (s_w)
print (s_h)

现在,这两种方法都具有与返回函数结果相同的效果,然后将其存储在一个包含两个值的变量中,作为元组,我们可以在以后引用.另外,我让函数将sh和sw的值保存在对象自己的变量中(标有self.variable).因此,您也可以使用x.sw和x.sh来引用它们.

Now both methods have the same effect as we return the result of the function and then store it in a variable that contains both values as a tuple we can reference later. Additionally I let the function save the values of sh and sw store in the Objects own variables (marked with self.variable). So you could as well reference them with x.sw and x.sh.

该函数现在是Ddata的方法,并在其自己的对象(由args方法的(self,...,...,...)声明)中工作.这样,您可以将值存储在名称中类的范围(对象.或者简单地返回它.您的选择.

The function is now a method of Ddata and works on its own object (declared by (self, ... , ... , ...) for the method args. That way you can store the values inside your name scope of the class(object. Or simply return it. Your choice.

这篇关于为什么要改变不可变的东西,如果它是全球性的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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