我一直在研究Python,我读了一章描述了它的None价值,但遗憾的是这本书在某些方面并不是很清楚.如果我在那里分享,我想我会找到问题的答案.
我想知道它的None价值是什么,你用它做什么?
而且,我没有得到这本书的这一部分:
分配的值
None给变量是将其重置到其原始的,空的状态的一种方法.
那是什么意思?
答案很棒,但由于我对计算机世界的了解不足(我还没有学过课程,对象等),所以我不理解大多数答案.这句话是什么意思?
分配的值
None给变量是将其重置到其原始的,空的状态的一种方法.
最后:
最后,我通过寻找不同的答案得到了答案.我必须感谢那些把时间用来帮助我的人(特别是Martijn Pieters和DSM),我希望我能选择所有答案作为最佳答案,但选择仅限于一个.所有答案都很棒.
我在运行python脚本时收到此错误:
TypeError: cannot concatenate 'str' and 'NoneType' objects
Run Code Online (Sandbox Code Playgroud)
我很确定'str'意味着字符串,但我不知道'NoneType'对象是什么.我的脚本在第二行开始了,我知道第一行是有效的,因为来自该行的命令在我的asa中,正如我所期望的那样.起初我以为可能是因为我在send_command中使用变量和用户输入.
'CAPS'中的所有内容都是变量,'小写'中的所有内容都是从'parser.add_option'选项输入的.
我正在使用pexpect和optparse
send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
send_command(child, SNMPSRVUSRCMD + snmpuser + group + V3AUTHCMD + snmphmac + snmpauth + PRIVCMD + snmpencrypt + snmppriv)
Run Code Online (Sandbox Code Playgroud) 我有一个MyThread课程.我有一个方法样本.我试图从具有相同的对象上下文运行它.请看一下代码:
class myThread (threading.Thread):
def __init__(self, threadID, name, counter, redisOpsObj):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
self.redisOpsObj = redisOpsObj
def stop(self):
self.kill_received = True
def sample(self):
print "Hello"
def run(self):
time.sleep(0.1)
print "\n Starting " + self.name
self.sample()
Run Code Online (Sandbox Code Playgroud)
看起来很简单不是吗.但是当我运行它时,我得到了这个错误
AttributeError: 'myThread' object has no attribute 'sample'现在我有那种方法,就在那里.那有什么不对?请帮忙
编辑:这是堆栈跟踪
Starting Thread-0
Starting Thread-1
Exception in thread Thread-0:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: …Run Code Online (Sandbox Code Playgroud) 我有这段自称的代码:
def get_input():
my_var = input('Enter "a" or "b": ')
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
else:
return my_var
print('got input:', get_input())
Run Code Online (Sandbox Code Playgroud)
现在,如果我输入"a"或"b",一切都很好.输出是:
Type "a" or "b": a
got input: a
Run Code Online (Sandbox Code Playgroud)
但是,如果我输入其他内容然后输入"a"或"b",我会得到:
Type "a" or "b": purple
You didn't type "a" or "b". Try again.
Type "a" or "b": a
got input: None
Run Code Online (Sandbox Code Playgroud)
我不知道为什么get_input()要回来None,因为它应该只返回my_var.print语句显示None正确的值,但函数由于某种原因不返回该值.
我在类似的错误消息上看到了其他一些帖子,但找不到可以解决我的问题的解决方案.
我用TkInter稍微涉足并创建了一个非常简单的UI.该守则如下─
from tkinter import *
root = Tk()
def grabText(event):
print(entryBox.get())
entryBox = Entry(root, width=60).grid(row=2, column=1, sticky=W)
grabBtn = Button(root, text="Grab")
grabBtn.grid(row=8, column=1)
grabBtn.bind('<Button-1>', grabText)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我启动并运行UI.当我单击Grab按钮时,我在控制台上收到以下错误:
C:\Python> python.exe myFiles\testBed.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "myFiles\testBed.py", line 10, in grabText
if entryBox.get().strip()=="":
AttributeError: 'NoneType' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
错误追溯到entryBox.
我敢肯定有人可能以前处理过这个问题.任何帮助表示赞赏.
list1 = ["name1", "info1", 10]
list2 = ["name2", "info2", 30]
list3 = ["name3", "info3", 50]
MASTERLIST = [list1, list2, list3]
def printer(list):
print ("Available Lists:")
listlen = (len(list))
for x in range(listlen):
print (list[x])[0]
Run Code Online (Sandbox Code Playgroud)
当我尝试运行时,此代码返回"'NoneType'对象不可订阅"错误printer(MASTERLIST).我做错了什么?
我对导致这种情况的思维过程感兴趣.对我来说,一个相对新手,似乎阻碍了,因为它阻止了列表处理的"链接"(例如mylist.reverse().append('a string')[:someLimit]).我想可能是"那些人的力量"决定列表理解是一个更好的范例(一个有效的观点),所以不想鼓励其他方法 - 但是这似乎有悖常理,以防止一种直观的方法,即使更好存在替代品.
请注意,我不是在抱怨(我敢肯定有是一个合理的理由,我在它是什么只是感兴趣!),也不是寻找一个解决方案(意见在这里非常具有启发性) -只是寻找一些情况下,并深入理解语言的设计过程.
我正在使用 python,我需要类中的函数,例如
class asas(object):
def b(self):
self.name = "Berkhan"
a = asas()
a.b().name
Run Code Online (Sandbox Code Playgroud)
我检查这个模块
Traceback (most recent call last):
File "C:\Users\Berkhan Berkdemir\Desktop\new 1.py", line 5, in <module>
a.b().name
AttributeError: 'NoneType' object has no attribute 'name'
Run Code Online (Sandbox Code Playgroud)
我应该怎么办?
如何解决错误AttributeError: \'NoneType\' object has no attribute \'click\'?它的失败在于self.home.get_you_button().click(). 当我没有创建页面对象类时,它\xe2\x80\x99s 工作正常...它单击“You”按钮,没有任何错误,但通过使用 POM,它\xe2\x80\x99s 失败。网址是https://huew.co/
代码试验:
\n\nfrom selenium.webdriver.support import expected_conditions\nfrom selenium.webdriver.support.wait import WebDriverWait\n\nclass HomePage():\n\n def __init__(self,driver):\n self.driver = driver\n\n def wait_for_home_page_to_load(self):\n wait =WebDriverWait(self.driver,30)\n wait.until(expected_conditions.visibility_of(self.driver.find_element_by_tag_name(\'html\')))\n\n def get_you_button(self):\n\n try:\n element = self.driver.find_element_by_xpath("//div[@class=\'desktop-public-header\']/a[@ng-controller=\'UserNavigationInteractionCtrl\'][6]")\n\n except:\n return None\nRun Code Online (Sandbox Code Playgroud)\n location = geolocator.geocode(" ".join(address.values()))
if location.longitude is not None:
node['pos'] = [location.longitude, location.latitude]
Run Code Online (Sandbox Code Playgroud)
不明白的方式我仍然得到这个错误:
File "/home/easypc/Documents/Udacity_nano_degree/Data_Wrangling/audit_vilnius.py", line 167, in shape_element
if location.longitude is not None:
AttributeError: 'NoneType' object has no attribute 'longitude'
Run Code Online (Sandbox Code Playgroud) 我已经达到了Mozilla Django教程的第4章,但后来我遇到了这个错误.我按照它说的所有内容进行了操作,但是当我尝试从管理面板打开BookInstance模型时,它给了我这个错误:
/ admin/catalog/bookinstance /'NoneType'对象中的AttributeError没有属性'id'
这是我的代码,models.py(我突出显示了发生错误的部分):
from django.db import models
from django.core.urlresolvers import reverse
class Book(models.Model):
"""
Model representing a book (but not a specific copy of a book).
"""
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
# Foreign Key used because book can only have one author, but authors can have multiple books
# Author as a string rather than object because it hasn't been declared yet in file.
summary = models.TextField(max_length=1000, help_text="Enter a …Run Code Online (Sandbox Code Playgroud)