下面的代码给出了不同的输出Python2和在Python3:
from sys import version
print(version)
def execute(a, st):
b = 42
exec("b = {}\nprint('b:', b)".format(st))
print(b)
a = 1.
execute(a, "1.E6*a")
Run Code Online (Sandbox Code Playgroud)
Python2 打印:
2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
('b:', 1000000.0)
1000000.0
Run Code Online (Sandbox Code Playgroud)
Python3 打印:
3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)]
b: 1000000.0
42
Run Code Online (Sandbox Code Playgroud)
为什么Python2将函数b内部的变量绑定到execute函数字符串中的exec值,Python3而不执行此操作?我怎样才能实现Python2in 的行为Python3?我已经尝试将全局和本地的字典传递给exec函数Python3,但到目前为止还没有任何工作.
---编辑---
在阅读了Martijns的回答后,我进一步分析了这一点 …
如果变量指定为浮点类型a,则abs可以使用该函数。以下示例正在运行:
fn main() {
let a = -1.0f64;
println!("{:?}", a.abs());
}
Run Code Online (Sandbox Code Playgroud)
1它按预期打印。但如果f64省略,则会在编译期间引发错误,如下例所示:
fn main() {
let a = -1.0;
println!("{:?}", a.abs());
}
Run Code Online (Sandbox Code Playgroud)
该版本出现以下故障:
Compiling playground v0.1.0 (file:///C:/git/Rust/playground)
src\main.rs:3:24: 3:29 error: no method named `abs` found for type `_` in the current scope
src\main.rs:3 println!("{:?}", a.abs());
^~~~~
note: in expansion of format_args!
<std macros>:2:25: 2:56 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: …Run Code Online (Sandbox Code Playgroud) 我定义了三个应该更改全局变量的函数x.
def changeXto1():
global x
x = 1
def changeXto2():
from __main__ import x
x = 2
def changeXto3():
import __main__
__main__.x = 3
x = 0
print x
changeXto1()
print x
changeXto2()
print x
changeXto3()
print x
Run Code Online (Sandbox Code Playgroud)
它给出了结果:
0
1
1
3
Run Code Online (Sandbox Code Playgroud)
changeXto1使用正常的全局语句.结果如预期x== 1. changeXto2用于from __main__ import解决x.这不起作用.之后x仍然是1. changeXto3用于import main解决x通过__main__.x.之后的结果是预期的3.
为什么不from __main__ import工作changeXto2,而import __main__在工作changeXto3 …
我尝试定义一个生成器函数mycount(),可以使用生成器函数重置,send(0)如下例所示.一切正常,除非我使用send(0)尚未启动的新生成器对象.在这种情况下,它给出了一个TypeError.是否有任何函数检查生成器是否已启动,或者我是否必须捕获TypeError并mycount(0)在这种情况下创建新的生成器对象?
def mycount(value):
while True:
v = yield value
if v == None:
value = value + 1
else:
value = v
g = mycount(3)
print(next(g)) # prints 3
print(next(g)) # prints 4
print(g.send(0)) # prints 0
print(next(g)) # prints 1
print(next(g)) # prints 2
g2 = mycount(3)
g2.send(0)
# TypeError: can't send non-None value to a just-started generator
Run Code Online (Sandbox Code Playgroud) 我刚开始学习Rust.在我使用这种语言的第一步中,我发现了一种奇怪的行为,当在内部main或另一个函数中执行迭代时,如下例所示:
fn myfunc(x: &Vec<f64>) {
let n = x.len();
println!(" n: {:?}", n);
for i in -1 .. n {
println!(" i: {}", i);
}
}
fn main() {
for j in -1 .. 6 {
println!("j: {}", j);
}
let field = vec![1.; 6];
myfunc(&field);
}
Run Code Online (Sandbox Code Playgroud)
而在循环main显示正常,没有打印的内部循环myfunc,我得到以下的输出:
j: -1
j: 0
j: 1
j: 2
j: 3
j: 4
j: 5
n: 6
Run Code Online (Sandbox Code Playgroud)
这种行为的原因是什么?
我刚开始学习C++和使用中遇到的矛盾gnu,一方面编译器和使用Visual C++,并Intel compiler在另一方面.下面的示例定义了一个Person带有指向std :: string的指针的类Name.在方法内Person::set,字符串由值分配.我确信更好的方法是使用指针,但这不是问题.
#include <iostream>
#include <string>
class Person
{
std::string *Name;
public:
Person(std::string *n); //Constructor
void print();
void set(std::string n);
};
Person::Person(std::string *n) : Name(n) //Implementation of Constructor
{
}
// This method prints data of person
void Person::print()
{
std::cout << *Name << std::endl;
}
void Person::set(std::string n)
{
Name = &n;
}
int main()
{
std::string n("Me");
std::string n2("You");
Person Who(&n); …Run Code Online (Sandbox Code Playgroud)