I want to make sure that the object has certain attributes in Python

Asked 2 years ago, Updated 2 years ago, 17 views

We're going to use the code below How do I create a function that acts as exist(Attr)?

a = myClass()
if a.exist(Attr):
    pass
else :
    pass

python attribute

2022-09-22 10:58

1 Answers

hasattr(obj, 'property') You can use it with

try:
    obj.attr()
    ... #Executive if present
catch:
    ... #If it doesn't exist, it's

The second of the two is faster. Use it at your convenience.


2022-09-22 10:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.