我在CNN的一个基础节目中遇到了这个错误:
AttributeError:'SparseCategoricalCrossentropy‘对象没有属性'name’
我以为你可以把loss=tf.metrics.SparseCategoricalCrossentropy()设为model.compile()
import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
tf.random.set_seed(42)
t
我有一个对象myobject,它可能返回None。如果返回None,则不会返回属性id
a = myobject.id
因此,当myobject为None时,上面的字符串会产生一个AttributeError
AttributeError: 'NoneType' object has no attribute 'id'
如果myobject为None,那么我希望a等于None。如何在一行语句中避免此异常,例如:
a = default(myobject.id, None)
我使用此代码来获取用户加入服务器的日期 if answers['member_older'] != None:
for i in range (len(id)):
x = id[i]
account_memb = client.get_user(x)
print(account_memb)
joined = account_memb.joined_at
new_date_memb = datetime.today() - joined
member_old = new_
我正在尝试创建恶意软件分类器,但遇到错误
AttributeError: 'Series' object has no attribute 'Id. Not sure of the error.
Traceback (most recent call last):
File"C:/Users/Afiqmatters/PycharmProjects/MajorProject/feature_extraction.py", line 23, in <module>
rids = [mids.loc[i].Id for i in rchoice
给予:
In [37]: class A:
....: f = 1
....:
In [38]: class B(A):
....: pass
....:
In [39]: getattr(B, 'f')
Out[39]: 1
好吧,要么叫超级还是爬mro?
In [40]: getattr(A, 'f')
Out[40]: 1
这是意料之中的。
In [41]: object.__getattribute__(A, 'f')
Out[41]: 1
In [42]: object.__getattri
我想在生成器和列表上运行嵌套循环。在循环中,我想访问每个元素的属性。
我发现了类似的关于遍历对象属性的代码问题,但是我无法解决我正在处理的问题。
下面是我的代码工作:
gen = api.search_submissions() # gen is generator of submissions
f_list = ['id','title']
sub_dict = {
'id':[],
'title':[]
}
for sub in gen:
for name in f_list:
s
我想添加按钮到盒布局,但在我的代码中有一些问题。单击“添加”按钮或“删除”按钮时,将收到AttributeError消息。谢谢你帮我。
我的python代码:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
class MainScreen(Screen):
pass
class SecondScreen(Screen):
pass
class ScreenManagement(Scre
我有一个简短的问题,我很好奇,如果我能为某些AttributeError设置例外,我找不到任何答案。
例如,我有这样的错误:
AttributeError: 'product.product' object has no attribute 'order_line'
我想只为这一个AttributeError设置异常,所以如果发生另一个AttributeError,我会看到错误消息。
我试过这样做:
except AttributeError == 'product.product' object has no attribute 'or
我在我的类中用实现了一个只写属性。奇怪的是,hasattr在这个属性的类和相应实例上的行为不同。
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
class User(Base, UserMixin):
# codes omitted...
@property
def password(self):
raise AttributeError("password is a wr
我一直在使用App Engine,但我似乎误解了NDB Datastore查询。我将抛出的错误放在查询的旁边。
在交互式控制台中执行以下操作:
from google.appengine.ext import ndb
class Client(ndb.Model):
email = ndb.StringProperty()
name = ndb.StringProperty(indexed=True)
#instantiated client instance with the parameters below. ID is 657837806898380
我正在开发一个机器人,它将在reddit上搜索删除操作的审核日志,但我缺少subreddit().mod.log()方法的文档。
到目前为止,我能从其中检索到的只有执行该操作的版主和该操作所在的subreddit,这两者对这个项目都没有帮助。我需要访问帖子ID以及此项目的帖子作者。
现在,我想这是可行的:
for i in r.subreddit(subbie).mod.log(action='removelink'):
print i.author
然而,我只是得到了一个错误:AttributeError: 'ModAction' object has
问题
使用mock.patch和autospec=True对类进行修补并不能保留该类实例的属性。
The Details
我正在尝试测试一个类Bar,它将类Foo的实例实例化为名为foo的Bar对象属性。正在测试的Bar方法称为bar;它调用属于Bar的Foo实例的方法foo。在测试时,我是在模仿Foo,因为我只想测试Bar是否访问了正确的Foo成员:
import unittest
from mock import patch
class Foo(object):
def __init__(self):
self.foo = 'foo'
class
例如,下面的代码是Python:
a = object()
a.b = 3
抛出AttributeError: 'object' object has no attribute 'b'
但是,下面这段代码:
class c(object): pass
a = c()
a.b = 3
挺好的。为什么我可以赋值属性b,而类x没有该属性?如何让我的类只定义属性?