简体   繁体   English

在 Python 中同时赋值和打印

[英]Assign and print at the same time in Python

In Python, is there a way to assign a result to a variable and immediately print it?在 Python 中,有没有办法将结果分配给变量并立即打印它? In other words, I am looking for a one-line equivalent to换句话说,我正在寻找一个相当于

a = something()
print(a)

(just once, not every assignment should be automatically printed). (仅一次,并非每个作业都应自动打印)。

There are REPLs, eg for Scala, where this happens automatically:有 REPL,例如对于 Scala,这是自动发生的:

scala> val count = 1
count: Int = 1

Well, if you just want to create a new variable and print the value of that variable like you've shown us, you could have a simple function to print and return the value:好吧,如果您只想创建一个新变量并像向我们展示的那样打印该变量的值,您可以使用一个简单的函数来打印并返回该值:

def print_and_return(value):
    print(value)
    return value

And now you can use it like this:现在你可以像这样使用它:

>>> a = print_and_return(5)
5
>>> print(a)
5

An other way, which you should never use, is to use the globals() dict.另一种永远不应该使用的方法是使用globals()字典。

I repeat, you should never do this.我再说一遍,你永远不应该这样做。

def print_and_set(name, value):
    globals()[name] = value
    print(globals()[name])

Now you can call it using:现在您可以使用以下方法调用它:

print_and_set('a', 5)

And it will print 5 and you can also access the variable a in the global namespace.它将打印5 ,您还可以访问全局命名空间中的变量a

从技术上讲,这是一行:

a = something(); print(a)

This is missing from python and for a good reason i guess.这在 python 中丢失了,我想这是有充分理由的。 In C you could have a valid statement like if a=30 { ... } because any assignment statement would have the assigned value , which could lead someone seeing the code for the first time scratching their heads.在 C 中,你可以有一个有效的语句,比如if a=30 { ... }因为任何赋值语句都会有赋值的值,这可能会让第一次看到代码的人摸不着头脑。

Also, baking your own assign function would be problematic since you get a reference copy inside the function body and not the actual variable此外,烘烤您自己的分配函数会出现问题,因为您在函数体内获得了一个引用副本,而不是实际变量

TL;DR; TL; 博士; -> no, there isn't -> 不,没有

Technically you can "print" and assign on the same line by using sys.从技术上讲,您可以使用 sys.path 在同一行“打印”和分配。 See the following:请参阅以下内容:

>>> import sys
>>> a = sys.stdout.write("hello")
hello
>>> print(a)
5

As you can see, you "print" - by writing to standard out which is essentially the the same as print() - the string and assign the length of the string to the variable - not the string itself.如您所见,您可以“打印” - 通过写入标准输出,这与 print() 本质上相同 - 字符串并将字符串的长度分配给变量 - 而不是字符串本身。

As of Python 3.8, the answer to this has changed.从 Python 3.8 开始,这个问题的答案已经改变。 Now, you can assign a value to a variable and return that same value using "assignment expressions" (colloquially called "the Walrus operator").现在,您可以为变量赋值并使用“赋值表达式”(俗称“海象运算符”)返回相同的值。

So, this is valid Python:所以,这是有效的 Python:

print("Hello, {}".format((w := "world")) print("你好,{}".format((w := "world"))

Which will print "Hello, world" and assign the string "world" to the variable "w" all in one line.它将打印“Hello,world”并将字符串“world”分配给变量“w”,全部在一行中。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM