Skip to content
Advertisement

Tag: attributes

How do you best mark node as visited in Depth First Search?

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

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.

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

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

Advertisement