Magic methods are implicitly invoked by a programming language when some event or language construct is used.
Questions tagged [magic-methods]
545 questions
15
votes
3 answers
__new__ method giving error object.__new__() takes exactly one argument (the type to instantiate)
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 __init__(self, a, b):
…
![](../../users/profiles/12491552.webp)
Danish Mahmood
- 307
- 2
- 7
15
votes
1 answer
How to use interfaces and magic methods in PHP
I want to use interfaces, but some of my implementations rely on magic methods such as __invoke and __call. I have to remove signatures of methods that may be invoked magically (in any implementation) from the interface. Which leads to the anti…
![](../../users/profiles/222015.webp)
Emanuel Landeholm
- 1,356
- 1
- 14
- 20
15
votes
2 answers
Check if a property exists on magically set properties
There is a lot of SO questions about the subject, notably this one, but it does not help me.
There is an ambiguity between property_exists and isset so before asking my question, I'm going to pointing it out:
property_exists
property_exists checks…
![](../../users/profiles/731138.webp)
Alain Tiemblo
- 32,952
- 14
- 114
- 147
15
votes
4 answers
PHP Arrayable Interface
I'm sure I read a while back about a new feature of PHP that was either a new magic method or a new interface so that you could implement Arrayable methods.
eg
interface Arrayable
{
public function toArray();
}
Was I imagining it?
![](../../users/profiles/225784.webp)
gawpertron
- 1,761
- 3
- 20
- 37
14
votes
2 answers
How to write a static python getitem method?
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.
![](../../users/profiles/572616.webp)
Woltan
- 12,751
- 12
- 70
- 97
14
votes
3 answers
Why PHP uses static methods in object context?
I have the following code (like, for real, this is my real code) :
foo() it displays foo.
Why would PHP try to use a static…
![](../../users/profiles/598404.webp)
Maxime Fabre
- 2,222
- 2
- 19
- 24
14
votes
4 answers
How to implement __iadd__ for a Python property
I'm trying to create a Python property where in-place adding is handled by a different method than retrieving the value, adding another value and reassigning. So, for a property x on an object o,
o.x += 5
should work differently than
o.x = o.x +…
![](../../users/profiles/172999.webp)
ptomato
- 51,511
- 13
- 105
- 149
13
votes
2 answers
Yii's magic method for controlling all actions under a controller
Commando need's help from you.
I have a controller in Yii:
class PageController extends Controller {
public function actionSOMETHING_MAGIC($pagename) {
// Commando will to rendering,etc from here
}
}
I need some magic method under…
user798596
13
votes
0 answers
Magic Methods in JavaScript
Are the any magic(al) methods under JavaScript?
I'm explicitly interested in setters, getters and callables (functions/methods), like those we have in PHP: __set, __get and __call && __callStatic.
I was googling and searching StackOverflow, but no…
![](../../users/profiles/748808.webp)
metaforce
- 1,257
- 3
- 16
- 26
13
votes
6 answers
Practical applications of PHP magic methods - __get, __set, and __call
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 read, so I have to ask: is there any consensus on when…
![](../../users/profiles/399584.webp)
Major Productions
- 5,522
- 10
- 63
- 138
13
votes
2 answers
Custom double star operator for a class?
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, players=None):
self.players = players or…
![](../../users/profiles/2505645.webp)
Markus Meskanen
- 15,203
- 11
- 57
- 106
12
votes
3 answers
How to implement __isset() magic method in PHP?
I'm trying to make functions like empty() and isset() work with data returned by methods.
What I have so far:
abstract class FooBase{
public function __isset($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter))
…
![](../../users/profiles/376947.webp)
Alex
- 60,472
- 154
- 401
- 592
12
votes
0 answers
List of all Python dunder methods - Which ones do you need to implement to correctly proxy an object?
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(x), x[], bool(x) require other dunder methods like…
![](../../users/profiles/2570622.webp)
Pragy Agarwal
- 385
- 4
- 22
12
votes
1 answer
Why are explicit calls to magic methods slower than "sugared" syntax?
I was messing around with a small custom data object that needs to be hashable, comparable, and fast, when I ran into an odd-looking set of timing results. Some of the comparisons (and the hashing method) for this object simply delegate to an…
![](../../users/profiles/2069350.webp)
Henry Keiter
- 15,543
- 7
- 45
- 76
12
votes
2 answers
Python class method chaining
To avoid getting lost in architectural decisions, I'll ask this with an analogous example:
lets say I wanted a Python class pattern like this:
queue = TaskQueue(broker_conn)
queue.region("DFW").task(fn, "some arg")
The question here is how do I…
![](../../users/profiles/371178.webp)
NFicano
- 1,005
- 10
- 26