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.
Questions tagged [python-datamodel]
99 questions
3
votes
5 answers
python ImportError: cannot import name 'Faker' from 'faker'
hello so I've been writing this script to pre-populate my Django database but when I stopped writing it I got a weird error:
My Script:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_project.settings')
import…

Alireza
- 135
- 1
- 10
3
votes
1 answer
What data model methods implement argument keyword unpacking?
I have a class that I would like to be able to unpack into an argument list using the *args and **kwargs syntax.
class MyFoo:
x = 1
y = 2 # unpack x and y
z = 3 # do not unpack z
def bar(x, y):
print(x, y)
def baz(a, b=2, x=100,…

AJMansfield
- 4,039
- 3
- 29
- 50
3
votes
1 answer
Why aren't all the names in dir(x) valid for attribute access?
Why would a coder stuff things into __dict__ that can't be used for attribute access? For example, in my Plone instance, dir(portal) includes index_html, but portal.index_html raises AttributeError. This is also true for the __class__ attribute of…

joeforker
- 40,459
- 37
- 151
- 246
3
votes
3 answers
How would you determine where each property and method of a Python class is defined?
Given an instance of some class in Python, it would be useful to be able to determine which line of source code defined each method and property (e.g. to implement 1). For example, given a module ab.py
class A(object):
z = 1
q = 2
def…

joeforker
- 40,459
- 37
- 151
- 246
3
votes
1 answer
How to count both sides of many-to-many relationship in Google App Engine
Consider a GAE (python) app that lets users comment on songs. The expected number of users is 1,000,000+. The expected number of songs is 5,000.
The app must be able to:
Give the number of songs a user has commented on
Give the number of users…

cope360
- 6,195
- 2
- 20
- 33
2
votes
1 answer
How can I access the weakref object of the class itself through the class?
As far as I know, __weakref__ is a descriptor defined in class, so that if it invoked from the instances of the class, it will give the weakref object:
from weakref import ref
class A:
pass
obj = A()
wr = ref(obj)
print(obj.__weakref__ is wr) …

S.B
- 13,077
- 10
- 22
- 49
2
votes
0 answers
Does the default min() method in python uses heap binary tree implementation to find the minimum value in a list?
Hi so i have a list u = [2,45,0,56,78,13].
We can use min() method to find the minimum value in a list. I was going through the heap binary tree.
The python has a module heapq and there is a method heapq.nsmallest() which helps to find the n…

Danish Xavier
- 1,225
- 1
- 10
- 21
2
votes
1 answer
Why are the class __dict__ and __weakref__ never re-defined in Python?
Class creation seems to never re-define the __dict__ and __weakref__ class attributes (i.e. if they already exist in the dictionary of a superclass, they are not added to the dictionaries of its subclasses), but to always re-define the __doc__ and…

Géry Ogam
- 6,336
- 4
- 38
- 67
2
votes
2 answers
Problem using super(python 2.5.2)
I'm writing a plugin system for my program and I can't get past one thing:
class ThingLoader(object):
'''
Loader class
'''
def loadPlugins(self):
'''
Get all the plugins from plugins folder
'''
from…

Diones
- 1,425
- 2
- 19
- 26
2
votes
1 answer
Proper use of __format__
I have a class that defines __str__ to return the integer value in hex and a __format__ to return the value formatted with the user's format spec:
class MyClass:
def __init__(self, value: int):
self._value = value
def…

Kyle
- 2,814
- 2
- 17
- 30
2
votes
2 answers
isinstance(type, object) = True, why?
I have an understanding that object is an instance of type and type inherits from object. So the following relationships makes sense:
isinstance(object, type) # returns True, ok
issubclass(type, object) # returns True, ok
I…

a-a
- 261
- 3
- 6
2
votes
3 answers
Unimplemented __init__()
I was looking over some code with a misspelled __init__() function (it had a total of 3 underbars instead of 4). And I realized I didn't really know what happens in a Python when an object cannot find a suitable __init__().
Is there a default…

Dylan Kirkby
- 1,427
- 11
- 19
2
votes
1 answer
How to make Python 2's __getitem__ work on a class?
How do I make item access ie. __getitem__ available on a class object in Python 2.x?
I've tried:
class B:
@classmethod
def __getitem__(cls, key):
raise IndexError
test:
B[0]
# TypeError: 'classobj' object has no attribute…

n611x007
- 8,952
- 8
- 59
- 102
2
votes
2 answers
Get class that defined method inside the method
How can I get the class that defined a method in Python?
For example
class A(object):
def meth(self):
return **get_current_class()**
class B(A):
def meth(self):
do_something
return super(B,self).meth()
>>> b=B()
>>>…

yjmade
- 441
- 4
- 9
2
votes
1 answer
decypher with me that obfuscated MultiplierFactory
This week on comp.lang.python, an "interesting" piece of code was posted by Steven D'Aprano as a joke answer to an homework question. Here it is:
class MultiplierFactory(object):
def __init__(self, factor=1):
self.__factor = factor
…

Nicolas Dumazet
- 7,147
- 27
- 36