关于如何知道对象在Python中是否具有属性和python判断对象的数据类型的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于'int'对象在python中不可调用、'str'对象在Pyth
关于如何知道对象在Python中是否具有属性和python判断对象的数据类型的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于'int'对象在python中不可调用、'str'对象在Python3中没有属性'decode'、actionscript(flex):如何知道对象的属性是否存在(或定义)?、Python中如何使用hasattr()函数判断对象是否具有某个属性等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- 如何知道对象在Python中是否具有属性(python判断对象的数据类型)
- 'int'对象在python中不可调用
- 'str'对象在Python3中没有属性'decode'
- actionscript(flex):如何知道对象的属性是否存在(或定义)?
- Python中如何使用hasattr()函数判断对象是否具有某个属性
如何知道对象在Python中是否具有属性(python判断对象的数据类型)
Python中是否有一种方法可以确定对象是否具有某些属性?例如:
>>> a = SomeClass()>>> a.someProperty = value>>> a.propertyTraceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: SomeClass instance has no attribute ''property''
在使用a属性property之前,如何知道该属性是否存在?
答案1
小编典典尝试hasattr()
:
if hasattr(a, ''property''): a.property
编辑:请参阅下面的zweiterlinde
的答案,他为寻求宽恕提供了很好的建议!一个非常pythonic
的方法!
python
中的一般做法是,如果大多数情况下该属性很可能存在,则只需对其进行调用,然后让该异常传播,或者使用try / except
块将其捕获。这可能会比快hasattr
。如果该属性在大多数时间可能不存在,或者你不确定,则使用该属性可能hasattr
比重复陷入异常块要快。
'int'对象在python中不可调用
我得到了这个,我期望它在打印x.withdraw()时能打印410。
Kyle 12345 500Traceback (most recent call last): File "bank.py", line 21, in <module> print x.withdraw()TypeError: ''int'' object is not callable
这是我的代码:
class Bank: def __init__(self, name, id, balance, withdraw): self.name = name self.id = id self.balance = balance self.withdraw = withdraw def print_info(self): return "%s %d %d" % (self.name, self.id, self.balance) def withdraw(self): if self.withdraw > self.balance: return "ERROR: Not enough funds for this transfer" elif self.withdraw < self.balance and self.withdraw >= 0: self.balance = self.balace - self.withdraw return self.balance else: return "Not a legitimate amount of funds"x = Bank("Kyle", 12345, 500, 90)print x.print_info()print x.withdraw()
我是否需要在类本身中修复某些问题,或者我的方法调用有问题?
答案1
小编典典您在实例上设置具有相同名称的属性:
self.withdraw = withdraw
您正在尝试调用的是该属性,而不是方法。Python不会区分方法和属性,它们也不位于单独的命名空间中。
为属性使用其他名称;withdrawn
(退出的过去时)作为更好的属性名称浮现在脑海:
class Bank: def __init__(self, name, id, balance, withdrawn): self.name = name self.id = id self.balance = balance self.withdrawn = withdrawn def print_info(self): return "%s %d %d" % (self.name, self.id, self.balance) def withdraw(self): if self.withdrawn > self.balance: return "ERROR: Not enough funds for this transfer" elif self.withdrawn < self.balance and self.withdrawn >= 0: self.balance = self.balance - self.withdrawn return self.balance else: return "Not a legitimate amount of funds"
(我也纠正了一个错字;您曾balace
在打算使用的位置使用过balance
)。
'str'对象在Python3中没有属性'decode'
我在python 3.3.4中遇到“解码”方法的问题。这是我的代码:
for lines in open(''file'',''r''): decodedLine = lines.decode(''ISO-8859-1'') line = decodedLine.split(''\t'')
但是我无法解码此问题的代码:
AttributeError: ''str'' object has no attribute ''decode''
你有什么想法?谢谢
答案1
小编典典一种 编码 字符串,另一种 解码 字节。
您应该从文件中读取字节并对其进行解码:
for lines in open(''file'',''rb''): decodedLine = lines.decode(''ISO-8859-1'') line = decodedLine.split(''\t'')
幸运的是,open
有一个编码参数使操作变得简单:
for decodedLine in open(''file'', ''r'', encoding=''ISO-8859-1''): line = decodedLine.split(''\t'')
actionscript(flex):如何知道对象的属性是否存在(或定义)?
我将ActionScript对象表现为hashmap,但是当对象没有给它异常的属性时:没有这样的变量。
在这里我期望它给了我null,而不是给出例外。那么你知道有没有办法处理它,也就是检查属性是否被定义为对象。
trace(obj [“2008-02”])//给出异常
解决方法
if (myObject.hasOwnProperty("propertyName"))
检查该属性是否存在。
编辑:也看看here。
Python中如何使用hasattr()函数判断对象是否具有某个属性
Python中如何使用hasattr()函数判断对象是否具有某个属性
在Python编程中,有时候我们需要判断某个对象是否具有某个属性。Python提供了一个内置函数hasattr()来帮助我们实现这一功能。在本文中,我们将介绍hasattr()函数的使用方法,并提供一些代码示例帮助读者理解和掌握这个函数的用法。
hasattr()函数的基本用法如下:
hasattr(obj, attr)
这个函数接受两个参数,第一个参数是待判断的对象,第二个参数是需要判断的属性名。函数返回一个布尔值,如果对象具有该属性,则返回True,否则返回False。
立即学习“Python免费学习笔记(深入)”;
接下来,让我们来看一些具体的示例。
示例1:判断对象的属性是否存在
class Person: def __init__(self, name, age): self.name = name self.age = age p = Person("Alice", 25) print(hasattr(p, "name")) # 输出True print(hasattr(p, "age")) # 输出True print(hasattr(p, "gender")) # 输出False
在这个示例中,我们定义了一个Person类,它具有name和age两个属性。我们创建了一个Person对象p,并使用hasattr()函数来判断对象p是否具有name、age和gender属性。通过运行上述代码,我们可以发现输出结果分别为True、True和False。
示例2:判断对象的方法是否存在
class Calculator: def __init__(self): self.result = 0 def add(self, x, y): return x + y def subtract(self, x, y): return x - y c = Calculator() print(hasattr(c, "add")) # 输出True print(hasattr(c, "subtract")) # 输出True print(hasattr(c, "multiply")) # 输出False
在这个示例中,我们定义了一个Calculator类,它具有add和subtract两个方法。我们创建了一个Calculator对象c,并使用hasattr()函数来判断对象c是否具有add、subtract和multiply方法。通过运行上述代码,我们可以发现输出结果分别为True、True和False。
示例3:判断内置类型的属性是否存在
s = "Hello, World!" print(hasattr(s, "lower")) # 输出True print(hasattr(s, "length")) # 输出False
在这个示例中,我们定义了一个字符串对象s。我们使用hasattr()函数来判断字符串对象s是否具有lower和length属性。通过运行上述代码,我们可以发现输出结果分别为True和False。
总结:
hasattr()函数是Python提供的一个很有用的判断对象是否具有某个属性的函数。它能够帮助我们编写更加健壮和灵活的代码。在实际的项目中,我们常常需要根据对象的属性来进行不同的操作,使用hasattr()函数能够帮助我们避免因为属性不存在而引发的错误。希望本文对读者在使用Python时能够有所帮助。
以上就是Python中如何使用hasattr()函数判断对象是否具有某个属性的详细内容,更多请关注php中文网其它相关文章!
关于如何知道对象在Python中是否具有属性和python判断对象的数据类型的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于'int'对象在python中不可调用、'str'对象在Python3中没有属性'decode'、actionscript(flex):如何知道对象的属性是否存在(或定义)?、Python中如何使用hasattr()函数判断对象是否具有某个属性等相关知识的信息别忘了在本站进行查找喔。
本文标签: