在Python3中,我想检查value是string还是None。
实现此目的的一种方法是
assert type(value) in { str, NoneType }
但是NoneType在Python中的位置是什么呢?
在不进行任何导入的情况下,使用NoneType生成NameError: name 'NoneType' is not defined。
我在其中一个Django模型中有一个自定义方法,它执行一些常规的DB数据获取和求和。这就是它:
from django.utils.timezone import utc, now
...
class Project(models.Model):
...
def getTotalMonthlyBackers(self, monthdate=now()):
from pledger.models import DonationHistory
return (DonationHistory.objects
我已经找了半天NoneType了。我把'print‘和dir()放在了由t2表示的对象的生成过程中。我使用'post mortem‘查看了崩溃后的数据结构,但没有找到NoneType。我想知道这是否可能是由代码的其他部分(如意算盘)引发的错误之一,我想知道是否有人能识别出这一点?( k2是一个'int‘)
File "C:\Python26\Code\OO.py", line 48, in removeSubtreeFromTree
assert getattr(parent, branch) is subtreenode
TypeError:
在使用PySQLPool时,我遇到了库的问题。它导致我得到以下异常:'TypeError: 'NoneType' object is not callable'。
我使用多处理创建一个池,并对一个虚拟函数调用Pool.map。在map的参数中,我包含了来自PySQLPool的连接。我已经包含了最低限度的代码,我可以用下面的代码来再现这个bug:
from config import *
import PySQLPool
import multiprocessing as mp
def a(b):
return b
PySQLPool.getNewPool
在openstack安装程序中尝试nova-list cli命令时,我得到了以下错误。NoneType‘object没有属性'getitem’
DEBUG (shell:777) 'NoneType' object has no attribute '__getitem__'
Traceback (most recent call last):
File "/opt/stack/python-novaclient/novaclient/shell.py", line 774, in main
Open
我已经在python中的另一个函数中定义了一个函数,现在我想调用内部函数。在python中,这是可能的吗?如何从func3调用func2
def func1():
def func2():
print("Hello!")
def func3():
# Enter code to call func2 here
请解释一下为什么我在使用python2.7的时候会得到一个错误和正确的输出。
def func1(func):
def func2():
print("Before func2")
func()
print("After func2")
return func2()
@func1
def func_d():
print("I am being decorated")
func_d()
输出为:
Before func2
I am being decorated
After
我正在使用Python中的Beautiful从HTML文件中刮取一些数据。在某些情况下,Beautiful返回包含string和NoneType对象的列表。我想过滤掉所有的NoneType对象。
在Python中,包含NoneType对象的列表是不可迭代的,因此列表理解不是其中的一个选项。具体来说,如果我有一个包含lis的列表NoneTypes,并且我尝试执行类似于[x for x in lis (some condition/function)]的操作,Python会抛出错误TypeError: argument of type 'NoneType' is not itera
我是Python新手。我试图为我的Raspberry Pi创建一个假的GPIO模块,它使用PWM电机,这样我的解释器(使用Visual代码)就可以理解它并无错误地传递它。
这就是我想要达到的目标:
#Motor.py
import RPi.GPIO as GPIO
GPIO.PWM(16,100).start(0)
这是我在尝试学习python处理模块的基本方法之后尝试创建的假模块。
#RPi/GPIO.py
#(RPi folder has an empty __init__.py file along with the GPIO.py file)
BOARD = 1
IN = 1
O
我正在尝试在列表中创建自定义排列(主要是在python中处理递归)。现在,当我运行我的代码时,我得到这个错误: TypeError: 'NoneType' object is not iterable 在我添加到列表复制之前,我得到了如下结果: AttributeError: 'NoneType' object has no attribute 'append' def findPermutations (size, max, curr_perm):
if not curr_perm or len(curr_perm) < siz
if story['epic']['id'] == epic['id']:
TypeError: 'NoneType' object is not subscriptable
代码:
for epic in epics:
if epic['archived'] is False:
print(epic['short_name'], epic['summary'], epic['story_count'])
for story
我尝试在一个方法中使用计时器,作为等待一段时间的方式,而不会阻塞脚本,就像time.sleep那样。 在类外部,计时器代码运行良好,但在类方法内部,它返回错误: TypeError:'NoneType‘对象不可调用 import time
from threading import Timer
openDuration = 10
### Valve class
class Valve():
def open(self, openDuration):
t = Timer(openDuration, Valve().dummyWait())
我遇到一些Keras函数,如下所示:函数(1*由逗号分隔的参数列表)(2*A参数)。但我不明白函数在第二个参数列表中的作用。我从未在Python中遇到过这样的函数。我看到的函数原型的常见类型如下:函数(参数列表)
这些函数的示例在Keras中实现:
x = Dense(128, activation='relu')(x) x = Dropout(0.35)(x) out = Dense(num_classes, activation='softmax')(x)
在本例中,该函数在应用新的更改之前,似乎考虑到了在x上所做的工作。
这种函数编写是否得到了Pyth
我试图让这个代码正常工作,但总是出现这个错误消息。代码如下:
def fatorial(n):
proximo = n - 1
n = n * proximo
while proximo > 1:
proximo -= 1
n = n * proximo
return print(n)
def coeficiente_binomial(n,k):
a = fatorial(n)
b = fatorial(k)
div = b * fatorial(n-k) // a
return print(d
这不是我的代码,而是这个网站的示例代码:
# Find PI to the Nth Digit
from math import *
digits = raw_input('Enter number of digits to round PI to: ')
# print ('{0:.%df}' % min(20, int(digits))).format(math.pi) # nested string formatting
# calculate pi using Machin-like Formula
print ('{0:.%df}
我正在编写一个带有定义函数的猜谜游戏程序。它要求用户想出一个介于1到1000之间的数字,然后程序试图在不到15次尝试中猜出这个数字。对于我的函数,为了在每个函数中返回多个值,我将它们作为列表返回。但是有一个NoneType错误--这是错误消息。
Traceback (most recent call last):
File "python", line 187, in <module>
File "python", line 167, in main
TypeError: 'NoneType' object is not su
我在Python 2.7中使用此代码通过以下方式生成一个新数字:
def Alg(n):
n=((n**2)-1)/4
return n
然后我得到了错误消息:
TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'
任何帮助都是最好的!谢谢!
使用Python4Delphi,将Delphi方法公开给Python,以便Python可以调用到Delphi应用程序中是相当简单的。但是,我无法将由Delphi方法创建的Python列表返回给Python。例如:
function TDelphiAPI.callMethod : PPyObject;
begin
// Create a new empty list of three elements
result := GetPythonEngine.PyList_New(3);
end;
import mylib
p = mylib.DelphiAPI()
print p.call