Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,是否有方法引用在=之后分配给的变量?类似于+=_Python_Python 3.x - Fatal编程技术网

在python中,是否有方法引用在=之后分配给的变量?类似于+=

在python中,是否有方法引用在=之后分配给的变量?类似于+=,python,python-3.x,Python,Python 3.x,Python有+=、-=、*=、%=,等等。这有一种通用形式吗?我想尝试的第一件事是: x = _ + 1 作为x+=1的通用版本。我对此很好奇,因为我的实际代码现在看起来是这样的: a = x[y][z] x[y][z] = f(a) 像x[y][z]=f()这样的东西在我的头脑中是有意义的,也许这是我只是想去那里,但不是。Python中有类似的东西吗?没有,对不起。我自己也曾多次希望得到类似的东西。不,没有,对不起。我自己也曾多次希望得到类似的东西。所以你可以这样做,有点。这是一个肮脏的

Python有+=、-=、*=、%=,等等。这有一种通用形式吗?我想尝试的第一件事是:

x = _ + 1
作为
x+=1
的通用版本。我对此很好奇,因为我的实际代码现在看起来是这样的:

a = x[y][z]
x[y][z] = f(a)

x[y][z]=f()
这样的东西在我的头脑中是有意义的,也许这是我只是想去那里,但不是。Python中有类似的东西吗?

没有,对不起。我自己也曾多次希望得到类似的东西。

不,没有,对不起。我自己也曾多次希望得到类似的东西。

所以你可以这样做,有点。这是一个肮脏的黑客虽然

import inspect, sys

def getVariableAtBeginningOfLine():
    lineNumber = inspect.currentframe().f_back.f_lineno
    with open(sys.argv[0]) as f:
        lines = f.read().split("\n")

    #now we have the line we call the function on. 
    line = lines[lineNumber-1]

    #do whatever we want with the line, in this case take the variable at the beginning of the line.
    return eval(line.split()[0])

x = 10

print x #10

x = getVariableAtBeginningOfLine() + 1

print x #11

所以你可以这样做。这是一个肮脏的黑客虽然

import inspect, sys

def getVariableAtBeginningOfLine():
    lineNumber = inspect.currentframe().f_back.f_lineno
    with open(sys.argv[0]) as f:
        lines = f.read().split("\n")

    #now we have the line we call the function on. 
    line = lines[lineNumber-1]

    #do whatever we want with the line, in this case take the variable at the beginning of the line.
    return eval(line.split()[0])

x = 10

print x #10

x = getVariableAtBeginningOfLine() + 1

print x #11

不。作业右侧的表达式不能知道作业的目标是什么。@BrenBarn当然可以!不。作业右侧的表达式不能知道作业的目标是什么。@BrenBarn当然可以!呵呵,有没有什么开放论坛可以向语言推荐新功能?@submarker建议可以进入python想法邮件列表,但你最好有(1)几个强大的用例,(2)一个具体的、好的(这有很多方面,有点主观)和其他不令人反感的语法。我的答案()有什么问题?除了非常可怕之外?好吧,它有点忽略了“省去你打字”这一点,这是类似的事情的全部要点。嘿,有没有任何形式的开放论坛来建议该语言的新功能?@submarker建议可以转到python想法邮件列表,但你最好有(1)几个强大的用例,以及(2)一个具体的、好的(这有很多方面,有点主观)和其他不令人反感的语法。我的答案()有什么问题吗?除了它非常可怕之外。好吧,它有点忽略了整个“省去打字”的意思我不确定我是否喜欢它,或者认为它太恶心而不被喜欢。@Subbarker我不确定我是否喜欢它,或者认为它太恶心而不被喜欢。