python Programming Glossary: hasattr
What is a metaclass in Python? http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python class as a parameter class '__main__.ObjectCreator' print hasattr ObjectCreator 'new_attribute' False ObjectCreator.new_attribute.. 'foo' # you can add attributes to a class print hasattr ObjectCreator 'new_attribute' True print ObjectCreator.new_attribute.. ... FooChild type 'FooChild' Foo 'echo_bar' echo_bar hasattr Foo 'echo_bar' False hasattr FooChild 'echo_bar' True my_foo..
In Python, how do I determine if an object is iterable? http://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable The only solution I have found so far is to call hasattr myObj '__iter__' But I am not sure how fool proof this is. .. the key is the attributes __getitem__ and __iter__ In 9 hasattr 1 2 3 4 '__iter__' Out 9 True In 11 hasattr 1 2 3 4 '__iter__'.. __iter__ In 9 hasattr 1 2 3 4 '__iter__' Out 9 True In 11 hasattr 1 2 3 4 '__iter__' Out 11 True In 12 hasattr u hello '__iter__'..
Flatten (an irregular) list of lists in Python http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python found in this question def flatten x result for el in x if hasattr el __iter__ and not isinstance el basestring result.extend flatten..
How do I get the path of the current executed file in python? http://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python are built in to the interpreter e.g. by py2exe return hasattr sys frozen def module_path encoding sys.getfilesystemencoding..
Is there any way to kill a Thread in Python? http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python the thread is not active # do we have it cached if hasattr self _thread_id return self._thread_id # no look for it in..
JSON datetime between Python and JavaScript http://stackoverflow.com/questions/455580/json-datetime-between-python-and-javascript comprehensive default handler function def handler obj if hasattr obj 'isoformat' return obj.isoformat elif isinstance obj .....
How to know if an object has an attribute in Python http://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python it python attributes share improve this question Try hasattr if hasattr a 'property' a.property EDIT See zweiterlinde's answer.. attributes share improve this question Try hasattr if hasattr a 'property' a.property EDIT See zweiterlinde's answer below.. with a try except block. This will likely be faster than hasattr . If the property is likely to not be there most of the time..
How do I check if a variable exists in Python? http://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists-in-python # myVar exists. To check if an object has an attribute if hasattr obj 'attr_name' # obj.attr_name exists. share improve this..
|