在Python中有没有一种方法来确定一个对象是否具有某些属性?例如:
>>> a = SomeClass()
>>> a.someProperty = value
>>> a.property
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: SomeClass instance has no attribute 'property'
在使用a之前,如何判断它
我面临着一个奇怪的问题,当转换一个pdf到图像。我在pdf hover .It上执行转换代码第一次运行良好,然后开始给我"ghostscript转换错误“从下一个time.The代码,给出错误如下所示。
int result = InitAPI(gsInstancePtr, args.Length, args);
它返回一个错误代码-100。完整代码如下所示。
public static void CallAPI(string[] args)
{
// Get a pointer to an instance of the Ghostscript API
class Foo
class Foo():
def __init__(self):
del self.foo
def foo(self):
pass
实例化raise的AttributeError: Foo instance has no attribute 'foo'。Foo由object (class Foo(object)) raises AttributeError: 'Foo' object attribute 'foo' is read-only派生而来。使用delattr(self
>>> class Foo:
... 'it is a example'
... print 'i am here'
...
i am here
>>> Foo.__name__
'Foo'
>>> Foo().__name__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no
当我试图访问对象"e“的任何方法时,我就会得到错误"AttributeError: instance没有属性”。我假设在创建对象时,我没有以正确的方式进行。有人知道为什么吗?
import shapefile
sf = shapefile.Reader('C:/users/name/desktop/shapefiles/Polygon')
e = shapefile.Editor(shapefile = 'desktop/shapefiles/Polygon.shp')
indexesMpart = [i for i, shape in en
我正在尝试使用delattr内置函数从Tkinter窗口派生的类实例中删除一个方法。但是,我得到了以下错误。我做错了什么?
错误:
AttributeError: Class instance has no attribute 'wm_title'
举个例子:
import Tkinter as tk
class Class (tk.Tk) :
def __init__ (self) :
tk.Tk.__init__(self)
# The method is clearly there, seeing as this works.
我在玩python来理解它是如何工作的,但是有一些奇怪的东西。我在我的__new__中定义了一个MetaClass方法,我希望第四个参数(它是类的属性块)包含在我的类中定义的方法。
我有这样的代码:
#!/usr/bin/python3
class MyMetaClass(type):
def __new__(metacls, name, bases, attrs):
instance = super(MyMetaClass, metacls) #Create instance of super class, which metaclass is type
我有一个Person类和一个SubPerson子类:
class Person:
def setname(self, name):
self.name = name
def display(self):
print(self.name)
class SubPerson(Person):
def display(self):
print("Name: {0}".format(self.name))
if __name__ == "__main__":
p = SubPers
我在python中的类中使用list有一个问题。下面是我的代码:
class Residues:
def setdata(self, name):
self.name = name
self.atoms = list()
a = atom
C = Residues()
C.atoms.append(a)
就像这样。我收到一个错误,说:
AttributeError: Residues instance has no attribute 'atoms'
请不要笑。我已经绝望了。
下面是一个带有getter和setter (来自)的python类的规范示例:
class Student(object):
# Initializer
def __init__(self, name):
# An instance variable to hold the student's name
self._name = name
# Getter method
@property
def name(self):
return self._name
#
使用nova版本"2“对安装程序和功能nova客户端的调用没有floating_ips属性。
from novaclient import client as NovaClient
class FloatingIpProvisioner():
def __init__(self, os_session):
self.nova = NovaClient.Client(version="2", session=os_session)
def AddFloatingIpToInstance(self, instance):
s
我有以下几种型号:
class A(Model):
pass
class B(Model):
a = ForeignKey(A, related_name = 'b')
b_field = CharField(max_length=64)
现在我想序列化一个A对象,其中我希望有第一个b对象。我以前有这样的代码:
class BSerializer(ModelSerializer):
class Meta:
model = B
fields = '__all__'
class ASerializ