Questions tagged [python-datamodel]

For questions about various details related to Python data model: built-in types, classes, metaclasses, magic __dunder__ methods, operators, object initialization, attribute lookup, etc. Always remember to use 'python' tag together with this one. Using specific tag like 'operators' or 'metaclass' when appropriate is encouraged.

99 questions
2
votes
2 answers

Python: Original 'object' class is redefined. How to get the 'object' class back?

I am using python language for Squish Automation Tool. This tool extends python with some custom objects and functions. This is what they say in the manual: Squish's Python-specific extension modules are loaded automatically by internally…
Alex Okrushko
  • 7,212
  • 6
  • 44
  • 63
1
vote
2 answers

How to represent the numbers 0 to 3, taking up 2 bits of memory in python

I'm writing code for a Deep Q Network in python. My computer has 32GB of memory but I run into significant issues as the training goes on because the replay buffer maxes out the RAM. I'm looking through the replay buffer code to see where I can…
1
vote
1 answer

classmethods and data model methods, implementing operands over classes or types

Here is what you would expect if you try and use the add operand over a type and int or between two type objects. >>> class Foo: ... pass ... >>> class Bar: ... pass ... >>> Foo + 1 Traceback (most recent call last): File "", line…
ekiim
  • 792
  • 1
  • 11
  • 25
1
vote
1 answer

Does a metaclass-level variable cascade down to the metaclass's instantiated classes's instantiated objects the way a class-level variable does?

I've been intensively consulting the Python docs for descriptors, and I can't wrap my head around some points in it regarding descriptor invocation and the given Python equivalent of object.__getattribute__(). The pure Python equivalent given (see…
1
vote
1 answer

Python: Filter iterable class

Is there a hook/dunder that an Iterable object can hold so that the builtin filter function can be extended to Iterable classes (not just instances)? Of course, one can write a custom filter_iter function, such as: def filter_iter(filt_func:…
thorwhalen
  • 1,920
  • 14
  • 26
1
vote
1 answer

What offers better performance for large datasets? Nested dictionaries or a dictionary of objects?

I find myself repeating this pattern when I am fetching from multiple database tables: records = {'p_key': { "record": r, "A": list(), "B": list(), "C" : list() } for r in db_records} I often have to group data this way because I cannot do joins…
Anthony O
  • 622
  • 7
  • 26
1
vote
1 answer

Example of a custom deleter method

I have come across various examples of a custom getter or setter, but what would be a use case of when a custom deleter would be used? So far an example that I have is just something like this: def __delattr__(self, attr): print('Deleting attr…
David542
  • 104,438
  • 178
  • 489
  • 842
1
vote
1 answer

Error handling in `__getattribute__` and `__getattr__`

Note this questions is answered in the comments I've noticed some behaviour with exception handling in __getattribute__ and __getattr__ that I can't find mentioned in the docs, or other posts on here. Consider this class A. If an instance of A has…
joel
  • 6,359
  • 2
  • 30
  • 55
1
vote
1 answer

Fast data structure in Python for indexing a bunch of images as duplicates

Introduction: I want to replace about 280'000 images of math formulas on the Encyclopedia of Mathematics by their corresponding TEX code. To do this, I have classified all of these images (or better: their URLs) into a list of 100'000 lists. Each…
1
vote
0 answers

Given a method, how to obtain the class that defines this method?

If this is a classmethod, this task is simple: class MyClass: @classmethod def some_fun(cls): pass fun = MyClass.some_fun my_class = fun.__self__ print (my_class is MyClass) # True However, it only works because some_fun is a…
user4385532
1
vote
3 answers

python __getattr__ help

Reading a Book, i came across this code... # module person.py class Person: def __init__(self, name, job=None, pay=0): self.name = name self.job = job self.pay = pay def lastName(self): return…
1
vote
1 answer

How can I "undefine" Python magic methods on a subclass?

Is there a way in Python 3 to indicate that a class does not support some operation that its parent class supports?* I know that classes can set __hash__ to None to indicate a type is unhashable, but this doesn't seem to work in general. For…
1
vote
4 answers

Python std methods hierarchy calls documented?

just encountered a problem at dict "type" subclassing. I did override __iter__ method and expected it will affect other methods like iterkeys, keys etc. because I believed they call __iter__ method to get values but it seems they are implemented…
David Unric
  • 7,421
  • 1
  • 37
  • 65
1
vote
2 answers

What does this Python code mean?

__author__="Sergio.Tapia" __date__ ="$18-10-2010 12:03:29 PM$" if __name__ == "__main__": print("Hello") print(__author__) Where does it get __main__ and __name__? Thanks for the help
delete
1
vote
5 answers

What is the easiest way to search through a list of dicts in Python?

My database currently returns a list of dicts: id_list = ({'id': '0c871320cf5111df87da000c29196d3d'}, {'id': '2eeeb9f4cf5111df87da000c29196d3d'}, {'id': '3b982384cf5111df87da000c29196d3d'}, {'id':…
ensnare
  • 40,069
  • 64
  • 158
  • 224