test1 = 0
def testFunc():
test1 += 1
testFunc()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
UnboundLocalError:赋值前引用的局部变量'test1'.
错误说这'test1'是局部变量,但我认为这个变量是全局的
那么它是全局的还是本地的,如何解决这个错误而不将全局test1作为参数传递给testFunc?
#it's python 3.2.3
class point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, point):
self.x += point.x
self.y += point.y
return self
def __repr__(self):
return 'point(%s, %s)' % (self.x, self.y)
class Test:
def __init__(self):
self.test1 = [point(0, i) for i in range(-1, -5, -1)]
self.test2 = [point(i, 0) for i in range(-1, -5, -1)]
print('%s\n+\n%s\n=\n%s' % (self.test1[0], self.test2[0], self.test1[0] + self.test2[0]))
test = Test()
input()
Run Code Online (Sandbox Code Playgroud)
这个程序的输出是
point(-1, -1)
+
point(-1, 0)
=
point(-1, -1)
Run Code Online (Sandbox Code Playgroud)
但它应该是
point(-1, …Run Code Online (Sandbox Code Playgroud) MonoDevelop建议转向:
if (someBoolVar)
anotherBoolVar = true;
Run Code Online (Sandbox Code Playgroud)
进入这个:
anotherBoolVar |= someBoolVar;
Run Code Online (Sandbox Code Playgroud)
它也做它,当我设置anotherBoolVar来false代替:
if (someBoolVar)
anotherBoolVar = false;
Run Code Online (Sandbox Code Playgroud)
变为:
anotherBoolVar &= !someBoolVar;
Run Code Online (Sandbox Code Playgroud)
有人可以解释这些陈述是如何平等的吗?
使用ApplicationContext这样编写程序的区别是什么:
using System;
using System.Windows.Forms;
namespace Test
{
class Test
{
static void Main(string[] args)
{
Application.Run(new Context(args));
}
}
class Context : ApplicationContext
{
public Context(string[] args)
{
//the program
Environment.Exit(1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和标准Main?
namespace Test
{
class Test
{
static void Main(string[] args)
{
//the program
}
}
}
Run Code Online (Sandbox Code Playgroud) 所以我有这门课:
class Test
{
private int field1;
private int field2;
public Test()
{
field1 = // some code that needs
field2 = // a lot of cpu time
}
private Test GetClone()
{
Test clone = // what do i have to write there to get Test instance
// without executing Test class' constructor that takes
// a lot of cpu time?
clone.field1 = field1;
clone.field2 = field2;
return clone;
}
}
Run Code Online (Sandbox Code Playgroud)
代码几乎解释了自己.我试图解决这个问题并想出了这个:
private Test(bool qwerty) {}
private Test GetClone() …Run Code Online (Sandbox Code Playgroud) 我使用创建我的文件File.WriteAllBytes().传递给的Byte [] File.WriteAllBytes()由我自己的算法加密.您需要在加密文件(程序用户知道密码)时使用的密码来解密它.但是当我的程序打开一些文件时File.ReadAllBytes(),有三种情况:
第一个很容易处理.第二个和第三个对我的程序是相同的,因为我的程序不知道某个随机文件的加密byte []和byte []之间的区别.
我如何区分这些情况?我想在将一些字节序列添加到byte []的结尾或开始之前将其传递给File.WriteAllBytes().这样安全吗?现代程序如何将文件与其他文件区分开来?
我有一个服务器程序和一个客户端程序。在开发程序时,为了方便起见,我在同一台计算机上运行服务器和客户端。服务器开始使用以下几行侦听传入连接:
var listener = new TcpListener(IPAddress.Any, 7070);
listener.Start();
Run Code Online (Sandbox Code Playgroud)
客户端使用以下几行连接到服务器(简化):
var client = new TcpClient(AddressFamily.InterNetwork);
client.Connect(IPAddress.Loopback, 7070);
Run Code Online (Sandbox Code Playgroud)
我使用是IPAddress.Loopback因为我在同一台机器上运行程序。但是,我知道服务器和客户端将来不一定运行在同一台计算机上,因此我将其从http://icanhazip.com ( IPAddress.Parse(...)) 更改为我的公共 IP。因此,客户端无法连接到同一台计算机上的服务器,出现异常No connection could be made because the target machine actively refused it <my public ip:7070>。
我尝试禁用防火墙,但它仍然不起作用。为什么服务器拒绝连接?我不是特意告诉它监听所有带有 的接口IPAddress.Any吗?
为什么会发生这种情况以及如何解决它?
我正在制作一个蛇游戏,要求玩家按下WASD按键而不停止游戏过程以获得玩家的输入.所以我不能input()用于这种情况,因为那时游戏停止滴答作响.
我找到了一个getch()函数,它可以在不按下输入的情况下立即输入,但是这个函数也会停止游戏滴答以获得输入input().我决定使用线程模块通过getch()不同的线程获取输入.问题是getch()在不同的线程中不工作,我不知道为什么.
import threading, time
from msvcrt import getch
key = "lol" #it never changes because getch() in thread1 is useless
def thread1():
while True:
key = getch() #this simply is almost ignored by interpreter, the only thing it
#gives is that delays print() unless you press any key
print("this is thread1()")
threading.Thread(target = thread1).start()
while True:
time.sleep(1)
print(key)
Run Code Online (Sandbox Code Playgroud)
那么为什么getch()它没有用thread1()呢?
c# ×5
python ×3
python-3.x ×3
client ×1
constructor ×1
file ×1
file-io ×1
forms ×1
instance ×1
monodevelop ×1
oop ×1
server ×1
tcp ×1
tcplistener ×1