Why don’t my subclass instances contain the attributes from the base class (causing an AttributeError when I try to use them)?

The subclass should be: class TypeTwoEvent(Event): def __init__(self, level=None, *args, **kwargs): super().__init__(*args, **kwargs) self.sr1 = level Because __init__ is overridden, the base class’ __init__ code will only run if it is explicitly requested. Despite its strange name, __init__ is not specially treated. It gets called automatically after the object is created; but otherwise it’s an … Read more

What is & How to use getattr() in Python?

Objects in Python can have attributes — data attributes and functions to work with those (methods). Actually, every object has built-in attributes (try dir(None), dir(True), dir(…), dir(dir) in Python console). For example you have an object person, that has several attributes: name, gender, etc. You access these attributes (be it methods or data objects) usually … Read more

How to check if an object has an attribute?

Try hasattr(): if hasattr(a, ‘property’): a.property See zweiterlinde’s answer below, who offers good advice about asking forgiveness! A very pythonic approach! The general practice in python is that, if the property is likely to be there most of the time, simply call it and either let the exception propagate, or trap it with a try/except … Read more

How to document class attributes in Python?

To avoid confusion: the term property has a specific meaning in python. What you’re talking about is what we call class attributes. Since they are always acted upon through their class, I find that it makes sense to document them within the class’ doc string. Something like this: class Albatross(object): “””A bird with a flight … Read more

React Js conditionally applying class attributes

The curly braces are inside the string, so it is being evaluated as string. They need to be outside, so this should work: <div className={“btn-group pull-right ” + (this.props.showBulkActions ? ‘show’ : ‘hidden’)}> Note the space after “pull-right”. You don’t want to accidentally provide the class “pull-rightshow” instead of “pull-right show”. Also the parentheses needs … Read more