python-datamodel Questions
32
Solved
So what I'm looking for here is something like PHP's print_r function.
This is so I can debug my scripts by seeing what's the state of the object in question.
Reprobation asked 10/10, 2008 at 16:19
4
Solved
I have a little helper class:
class AnyOf(object):
def __init__(self, *args):
self.elements = args
def __eq__(self, other):
return other in self.elements
This lets me do sweet magic like:
&...
Kenzie asked 30/11, 2017 at 15:51
2
Solved
I stumbled upon this code that I found weird as it seems to violate the fact that python builtins call dunder methods directly from the class of the object. Using __call__ as an example, if we defi...
Squawk asked 13/8, 2023 at 23:18
13
Solved
How can I get the class that defined a method in Python?
I'd want the following example to print "__main__.FooClass":
class FooClass:
def foo_method(self):
print "foo"
class BarClass(FooClass)...
Selfsatisfaction asked 7/6, 2009 at 2:18
25
Solved
What are metaclasses? What are they used for?
Braden asked 19/9, 2008 at 6:10
13
Solved
I want to be able to create a class (in Python) that once initialized with __init__, does not accept new attributes, but accepts modifications of existing attributes. There's several hack-ish ways ...
Edgell asked 30/8, 2010 at 19:20
12
Solved
How do I find out the name of the class used to create an instance of an object in Python?
I'm not sure if I should use the inspect module or parse the __class__ attribute.
Gualterio asked 4/2, 2009 at 11:37
19
Solved
Given that a function a_method has been defined like
def a_method(arg1, arg2):
pass
Starting from a_method itself, how can I get the argument names - for example, as a tuple of strings, like (&qu...
Qualified asked 20/10, 2008 at 14:22
13
Solved
For logging purposes I want to retrieve the fully qualified class name of a Python object. (With fully qualified I mean the class name including the package and module name.)
I know about x.__clas...
Alcahest asked 7/1, 2010 at 11:50
1
According to the object.__eq__() documentation, the default (that is, in the object class) implementation for == is as follows:
True if x is y else NotImplemented
Still following the documentation...
Vandalism asked 30/5, 2022 at 21:10
5
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_MODUL...
Twiddle asked 3/12, 2018 at 12:49
1
Solved
I'm trying to create an object proxy.
Attribute/property lookup can be done by simply implementing the __getattribute__, __setattr__ and __delattr__ methods.
However, other functionalities like len...
Ferebee asked 21/5, 2019 at 12:26
1
Solved
In Python, what is the rationale for which object.__setattr__ and type.__setattr__ raise an AttributeError during attribute update if the type has an attribute which is a data descriptor without a ...
Voluminous asked 22/3, 2021 at 21:27
5
Solved
Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?
Ptolemy asked 10/9, 2008 at 0:31
11
Solved
... the is keyword that can be used for equality in strings.
>>> s = 'str'
>>> s is 'str'
True
>>> s is 'st'
False
I tried both __is__() and __eq__() but they didn't w...
Hostile asked 7/6, 2010 at 8:17
3
Solved
Consider the two functions below:
def f1():
return "potato"
f2 = lambda: "potato"
f2.__name__ = f2.__qualname__ = "f2"
Short of introspecting the original source code, is there any way to dete...
Tildi asked 4/6, 2019 at 20:37
1
Solved
The docs say that values views are not treated as set-like, but sometimes they are:
>>> d = {1: 1}
>>> d.values() | d.keys()
{1}
>>> d.values() & d.keys()
{1}
>...
Fauver asked 1/5, 2019 at 17:19
4
Solved
I'd like to be able to do:
>>> class a(str):
... pass
...
>>> b = a()
>>> b.__class__ = str
Traceback (most recent call last):
File "<stdin>", line 1, in <modu...
Militant asked 10/6, 2010 at 9:45
4
Solved
I have a class where I want to override the __eq__ method. It seems to make sense that I should override the __ne__ method as well. Should I implement __ne__ as the negation of __eq__ as such or is...
Mariellamarielle asked 4/12, 2010 at 6:18
1
Solved
Why can't you override a class name declaratively, e.g. to use a class name which is not a valid identifier?
>>> class Potato:
... __name__ = 'not Potato'
...
>>> Potato.__name_...
Dziggetai asked 11/4, 2018 at 17:31
4
Solved
I've started to use the python descriptor protocol more extensively in the code I've been writing. Typically, the default python lookup magic is what I want to happen, but sometimes I'm finding I w...
Pentahedron asked 27/10, 2009 at 0:38
5
Solved
I am trying to implement slice functionality for a class I am making that creates a vector representation.
I have this code so far, which I believe will properly implement the slice but whenever I ...
Rawley asked 29/5, 2010 at 22:52
1
Solved
It's documented that the definition order in classes is preserved (see also PEP 520):
If the metaclass has no __prepare__ attribute, then the class namespace is initialised as an empty ordered m...
Alexipharmic asked 15/3, 2017 at 0:18
2
Solved
As I read Python answers on Stack Overflow, I continue to see some people telling users to use the data model's special methods or attributes directly.
I then see contradicting advice (sometimes f...
Uncovered asked 26/10, 2016 at 21:9
2
Solved
I'm using set() and __hash__ method of python class to prevent adding same hash object in set. According to python data-model document, set() consider same hash object as same object and just...
Biafra asked 18/7, 2016 at 6:54
1 Next >
© 2022 - 2024 — McMap. All rights reserved.