containers

Getting container/parent object from within python

Getting container/parent object from within python Question: In Python, is it possible to get the object, say Foo, that contains another object, Bar, from within Bar itself? Here is an example of what I mean class Foo(object): def __init__(self): self.bar = Bar() self.text = “Hello World” class Bar(object): def __init__(self): self.newText = foo.text #This is …

Total answers: 5

heapq with custom compare predicate

heapq with custom compare predicate Question: I am trying to build a heap with a custom sort predicate. Since the values going into it are of "user-defined" type, I cannot modify their built-in comparison predicate. Is there a way to do something like: h = heapq.heapify([…], key=my_lt_pred) h = heapq.heappush(h, key=my_lt_pred) Or even better, I …

Total answers: 7

Python equivalent for C++ STL vector/list containers

Python equivalent for C++ STL vector/list containers Question: Is there something similar in Python that I would use for a container that’s like a vector and a list? Any links would be helpful too. Asked By: pandoragami || Source Answers: You can use the inbuilt list – underlying implementation is similar to C++ vector. Although …

Total answers: 6