magic-methods Questions

7

Solved

I've generally tried to stay away from PHP's magic methods because they seem to obfuscate an object's public interface. That said, they seem to be used more and more, at least, in the code I've rea...
Alyssa asked 12/5, 2011 at 14:46

5

My problem is: I need to overload standard get and set for static variables in class... but no such functionality provided in php... it was asked in 2008 and still not implemented... Same goes for ...
Cameron asked 7/12, 2011 at 9:47

3

Solved

How does one implement custom double star operator (**) for unpacking, similar to how __iter__ works with single star operator (*)? For example: class PlayerManager(object): def __init__(self, ...
Rhombic asked 28/10, 2015 at 21:49

11

Solved

I have gone through most of the documentation of __getitem__() in the Python docs, but I am still unable to grasp the meaning of it. So all I can understand is that __getitem__() is used to impleme...
Chasse asked 26/4, 2017 at 7:15

3

Here I have defined immutable class str. In __new__ method I am changing the values of instances such as "hello" to uppercase. Why should we do it using __new__ when we can define u...
Implausible asked 9/12, 2021 at 9:1

13

Is there a magic method that can overload the assignment operator, like __assign__(self, new_value)? I'd like to forbid a re-bind for an instance: class Protect(): def __assign__(self, value): ...
Kirtley asked 13/6, 2012 at 23:13

3

Solved

In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the __getattribute__() method even of the object’s metaclas...
Racer asked 13/10, 2012 at 11:33

28

Solved

What is the difference between __str__ and __repr__ in Python?
Carleencarlen asked 17/9, 2009 at 4:27

16

Solved

I know that __call__ method in a class is triggered when the instance of a class is called. However, I have no idea when I can use this special method, because one can simply create a new method an...
Enclosure asked 28/4, 2011 at 20:54

4

Solved

I am playing with numpy and digging through documentation and I have come across some magic. Namely I am talking about numpy.where(): >>> x = np.arange(9.).reshape(3, 3) >>> np.w...
Discommon asked 12/4, 2011 at 22:39

4

Solved

Consider the following abhorrent class: class MapInt: __call__ = int def __sub__(self, other): return map(self, other) __add__ = map One can then call map(int, lst) via MapInt() - lst, i.e. ...
Drudge asked 22/8, 2022 at 18:47

0

Refer to Python documentation __class_getitem__: A class can generally only be parameterized if it defines the special class method __class_getitem__(). And typing.Type: Deprecated since version...
Option asked 22/7, 2022 at 2:58

4

Solved

I understand that there are magic methods in python that can be overwritten by classes to control the way certain built in functions treat the members of these classes. For example, the behavior of...
Incardinate asked 19/2, 2018 at 14:24

1

Solved

If your question was closed as a duplicate of this, it is because you had a code sample including something along the lines of either: class Example: def __int__(self, parameter): self.attribute ...
Mesomorph asked 2/6, 2022 at 21:0

2

Solved

As an example, consider the following: class FooMeta(type): def __len__(cls): return 9000 class GoodBar(metaclass=FooMeta): def __len__(self): return 9001 class BadBar(metaclass=FooMeta): ...
Susan asked 17/5, 2022 at 20:4

6

Solved

why the following code is giving error? class Foo: def __new__(cls, *args, **kwargs): print("Creating Instance") instance = super(Foo, cls).__new__(cls,*args, **kwargs) return instance def ...
Derbyshire asked 6/12, 2019 at 17:35

9

Solved

Where is a complete list of the special double-underscore/dunder methods that can be used in classes? (e.g., __init__, __new__, __len__, __add__)
Lacagnia asked 13/9, 2009 at 21:2

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...

6

I am trying to understand the __str__ method in Python. class Test: def __str__(self): return 5 t = Test() print(t.__str__()) In this method it returns an integer value but the print method is...
Bertelli asked 8/8, 2021 at 7:4

3

Solved

I heard from one guy that you should not use magic methods directly. and I think in some use cases I would have to use magic methods directly. So experienced devs, should I use python magic methods...
Showalter asked 5/4, 2020 at 5:16

1

Is there a "mind map", UML diagram, graphic, or some solid reference for the different python types and the magic methods they must implement? I'm using Python 3.8. The data model docs an...
Pachton asked 30/3, 2021 at 15:51

2

Solved

Say we have a class with several protected and/or public methods. I need to perform a check each time a method is called. I could do that check each time i call a method : class Object { // Metho...
Crept asked 23/11, 2012 at 18:36

4

Solved

I don't know what the __setstate__ and __getstate__ methods do, so help me with a simple example.
Auerbach asked 21/12, 2009 at 9:17

1

Solved

I am writing a class that defines __iter__ and __len__, where the value of __len__ depends on the iterator returned by __iter__. I am getting an interesting RecursionError. Language versions: Pytho...
Inexplicable asked 23/12, 2020 at 17:15

2

Solved

What do I need to change to make this work? class A: @staticmethod def __getitem__(val): return "It works" print A[0] Note that I am calling the __getitem__ method on the type A.
Grimona asked 31/5, 2011 at 12:59

© 2022 - 2024 — McMap. All rights reserved.