我有这个代码:
package tests;
import java.util.Hashtable;
public class Tests {
public static void main(String[] args) {
Hashtable<String, Boolean> modifiedItems = new Hashtable<String, Boolean>();
System.out.println("TEST 1");
System.out.println(modifiedItems.get("item1")); // Prints null
System.out.println("TEST 2");
System.out.println(modifiedItems.get("item1") == null); // Prints true
System.out.println("TEST 3");
System.out.println(Boolean.valueOf(null)); // Prints false
System.out.println("TEST 4");
System.out.println(Boolean.valueOf(modifiedItems.get("item1"))); // Produces NullPointerException
System.out.println("FINISHED!"); // Never executed
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我不明白为什么测试3工作正常(它打印false
并且不产生NullPointerException
)同时测试4抛出一个NullPointerException
.正如你在测试1和2中看到的那样,null
并且modifiedItems.get("item1")
是等于和null
.
Java 7和8中的行为相同.
我对这张照片中的错误感到困惑:
我不知道如何解决它们.我的程序是一个Python-Flask Web框架.当我使用VScode调试程序时,Pylint会显示这些错误.我知道这个问题无关紧要,但这让我很恼火.我该如何解决?
# -*- coding: utf-8 -*-
import sys
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_moment import Moment
#from flask_wtf import Form
#from wtforms import StringField, SubmitField
#from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemy
reload(sys)
sys.setdefaultencoding('utf-8')
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost:3306/test?'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)
if __name__ == '__main__':
db.create_all()
app.run()
Run Code Online (Sandbox Code Playgroud) 我有一个清单,a
:
a = ['a','b','c']
Run Code Online (Sandbox Code Playgroud)
并且需要复制一些带有以_ind
这种方式添加的后缀的值(顺序很重要):
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']
Run Code Online (Sandbox Code Playgroud)
我试过了:
b = [[x, x + '_ind'] for x in a]
c = [item for sublist in b for item in sublist]
print (c)
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']
Run Code Online (Sandbox Code Playgroud)
我认为我的解决方案有点过于复杂.有没有更好,更pythonic的解决方案?
我有一个Python脚本:
if True:
if False:
print('foo')
print('bar')
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行我的脚本时,Python提出了一个IndentationError
:
File "script.py", line 4
print('bar')
^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)
我一直在玩我的程序,我还能够产生其他三个错误:
IndentationError: unexpected indent
IndentationError: expected an indented block
TabError: inconsistent use of tabs and spaces in indentation
这些错误意味着什么?我究竟做错了什么?我该如何修复我的代码?
如果您已经注意到,python会在大指数结果的末尾添加一个L,如下所示:
>>> 25 ** 25
88817841970012523233890533447265625L
Run Code Online (Sandbox Code Playgroud)
做一些测试后,我发现,任何数量低于10不包括大号.例如:
>>> 9 ** 9
387420489
Run Code Online (Sandbox Code Playgroud)
这很奇怪,所以,为什么会发生这种情况,有什么方法可以阻止它吗?所有帮助表示赞赏!
精简版
我最近遇到了一些Python代码,其中函数的返回类型PyObject
在文档中指定.什么是PyObject
?
详细版本
我不是C/C++程序员,但是当我遇到PyObject
上面链接的文档时,Google告诉我这PyObject
是一个使用Python/C API定义的Python对象.具体来说,API文档定义PyObject如下:
所有对象类型都是此类型的扩展.这是一种类型,其中包含Python将对象指针视为对象所需的信息.在正常的"发布"版本中,它仅包含对象的引用计数和指向相应类型对象的指针.它对应于PyObject_HEAD宏扩展定义的字段.
坦率地说,我并不完全理解这一点,或者它是否回答了我的基本问题,但它并没有让我觉得将PyObject
一个对象视为一个完整的Python对象显然是错误的.另一方面,也许从技术上来说,PyObject
必须使用Python/C API将该类型创建为标准Python的扩展.例如,是一个整数a PyObject
?
可能相关的链接
http://docs.scipy.org/doc/numpy/reference/c-api.types-and-structures.html
我知道为了比较C中的两个字符串,您需要使用该strcmp()
函数.但我试图将两个字符串与==
运算符进行比较,并且它有效.我不知道如何,因为它只是比较两个字符串的地址.如果字符串不同,它应该不起作用.但后来我打印了字符串的地址:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* str1 = "First";
char* str2 = "Second";
char* str3 = "First";
printf("%p %p %p", str1, str2, str3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
00403024 0040302A 00403024
Process returned 0 (0x0) execution time : 0.109 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)
怎么可能str1
和str3
具有相同的地址?它们可能包含相同的字符串,但它们不是同一个变量.
我会定义价值.但是这个值可能是哈希键的值.如果此键不存在,我将使用rescue来定义值为nil.例如
foo = bar[:a][:b][:c] rescue nil
但在实践中告诉我糟糕的风格,因为我使用其修饰形式的救援.我将改变逻辑以使用检查三个条件.
foo = bar[:a][:b][:c] if bar.key?(:a) && bar[:a].key?(:b) && bar[:a][:b].key?(:c)
我真的很想知道为什么我们要避免在rails中使用其修改器形式的救援?
我正在编写一个函数来从python中的文本中分割数字和其他一些东西.代码看起来像这样:
EN_EXTRACT_REGEX = '([a-zA-Z]+)'
NUM_EXTRACT_REGEX = '([0-9]+)'
AGGR_REGEX = EN_EXTRACT_REGEX + '|' + NUM_EXTRACT_REGEX
entry = re.sub(AGGR_REGEX, r' \1\2', entry)
Run Code Online (Sandbox Code Playgroud)
现在,这段代码在python3中工作得非常好,但它在python2下不起作用并得到" 无法匹配的组 "错误.
问题是,我需要支持这两个版本,我无法让它在python2中正常工作,尽管我尝试了其他各种方法.
我很好奇这个问题的根源是什么,是否有任何解决方法?
在下面的代码中,我定义了两个函数。main
和cube
。我想main
成为我的程序的开始,所以我cube
在里面调用了main
:
>>> def main():
number = int(input('Enter a number: '))
cubed_number = cube(number)
print("The number cubed is: ", cubed_number)
>>> def cube(number):
return number * number * number
>>>
Run Code Online (Sandbox Code Playgroud)
但是我在定义cube
之后main
,所以我认为我的代码会引发一个NameError
. 然而,Python 并没有引发异常,而是完美地执行了我的代码:
>>> main()
Enter a number: 5
The number cubed is: 125
>>>
Run Code Online (Sandbox Code Playgroud)
发生了什么?为什么 Python 能够运行我的代码,而不知道它cube
是如何定义的?NameError
怎么没有升职?
更奇怪的是,当我尝试对类做同样的事情时,Python 确实引发了一个NameError
:
>>> class Main:
cubed_number()
Traceback (most recent call last): …
Run Code Online (Sandbox Code Playgroud)