In some algorithms, like DFS, we need to mark nodes as visited. If I have a Node class, in Python we can write something like node.visited = True. However when I do that, I change the nodes which might be undesired. So what would be the best and cleanest approach here? Things I thought of: delete the attribute at the
Tag: attributes
attributeError when calling method of object class
Trying to animate a car with button to accelerate and decelerate. When I press a button to accelerate/decelerate, I get an error in the ‘if’ statements on both methods “AttributeError: ‘bool’ object has no attribute ‘speed'”. Can someone help me figure out what’s going wrong? Thank you! Answer You should pass to clicked.connect a method bound to the object in
namedtuple._source not working in python 3.7
I’ve tried with this code But when I run it it says that there is no attribute _source (for t and therefore also for i). Has it been eliminated since 3.6? Answer Yes, as stated here, the attribute _source has been removed from namedtuples in Python 3.7. Changed in version 3.7: Remove the verbose parameter and the _source attribute.
Missing attribute `value` for equal ctypes array objects
I know 2 ways of creating a c array out of a python sequence data = (1, 2, 3, 4, 5, 6). Creating an array class, initializing it and passing the values through the value attribute: Creating an array class and passing the value as arguments during initialization: Both generates the same type: But when trying to access the value
getattr and setattr on nested subobjects / chained properties?
I have an object (Person) that has multiple subobjects (Pet, Residence) as properties. I want to be able to dynamically set the properties of these subobjects like so: Currently I get the wrong output: {‘pet’: <__main__.Pet object at 0x10c5ec050>, ‘residence’: <__main__.Residence object at 0x10c5ec0d0>, ‘pet.name’: ‘Sparky’, ‘residence.type’: ‘Apartment’} As you can see, instead of setting the name attribute on the
Adding an attribute to a Python dictionary from the standard library
I was wondering if you could add an attribute to a Python dictionary? Is there anyway to quickly add my description attribute without having to copy the dict class and overloading the constructor. I thought it would be simple, but I guess I was wrong. Answer Just derive from dict: Instances of MyDict behave like a dict, but can have
selecting attribute values from lxml
I want to use an xpath expression to get the value of an attribute. I expected the following to work but this gives an error : Am I wrong to expect this to work? Answer find and findall only implement a subset of XPath. Their presence is meant to provide compatibility with other ElementTree implementations (like ElementTree and cElementTree). The
Python: Make class iterable
I have inherited a project with many large classes constituent of nothing but class objects (integers, strings, etc). I’d like to be able to check if an attribute is present without needed to define a list of attributes manually. Is it possible to make a python class iterable itself using the standard syntax? That is, I’d like to be able
Extracting an attribute value with beautifulsoup
I am trying to extract the content of a single “value” attribute in a specific “input” tag on a webpage. I use the following code: I get TypeError: list indices must be integers, not str Even though, from the Beautifulsoup documentation, I understand that strings should not be a problem here… but I am no specialist, and I may have
Python dictionary from an object’s fields
Do you know if there is a built-in function to build a dictionary from an arbitrary object? I’d like to do something like this: NOTE: It should not include methods. Only fields. Answer Note that best practice in Python 2.7 is to use new-style classes (not needed with Python 3), i.e. Also, there’s a difference between an ‘object’ and a