Questions tagged [magic-methods]

Magic methods are implicitly invoked by a programming language when some event or language construct is used.

616 questions
21
votes
2 answers

PHPDoc and __callStatic

tl;dr What is the correct way to annotate (in PHPDoc) functions implemented via __callStatic? More important: is there a way that will make NetBeans and PHPStorm understand that these are static methods? Motivation If you want the bigger picture,…
abesto
  • 2,331
  • 16
  • 28
21
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…
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
21
votes
6 answers

How to make inline array initialization work like e.g. Dictionary initialization?

Why is it possible to initialize a Dictionary like this: var dict = new Dictionary() { { "key1", 1 }, { "key2", 2 } }; ...but not to initialize, say, an array of KeyValuePair objects in exactly the same way: var…
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
20
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?
gawpertron
  • 1,867
  • 3
  • 21
  • 37
19
votes
3 answers

__callStatic(): instantiating objects from static context?

I am confused about how "static" and "dynamic" functions and objects in PHP work together especially with regards to __callStatic(). How __callStatic() works: You can have a normal class MyClass, where within the class you can put a static…
Dennis
  • 7,907
  • 11
  • 65
  • 115
19
votes
1 answer

Paste a string as a variable, not as executable code snippet, into IPython

I'm aware of the magic IPython %paste command, which is quite useful, if you have valid code to insert. Now I don't want to insert code, I just want to store some string from the copy buffer as a variable. Is there a simpler way to do that, except…
Michael
  • 7,316
  • 1
  • 37
  • 63
18
votes
3 answers

What are the names of the magic methods for the operators "is" and "in"?

I would like to make bool binary operations using the magic methods for these operators. For example, I can get a < b as getattr(a, '__lt__')(b) or a == b as getattr(a, '__eq__')(b). Can I get a in b and a is b in such a way?
nik
  • 265
  • 2
  • 9
18
votes
2 answers

Triggering __call() in PHP even when method exists

The PHP documentation says the following about the __call() magic method: __call() is triggered when invoking inaccessible methods in an object context. Is there a way I can have __call() called even when a method exists, before the actual method…
Chad Johnson
  • 21,215
  • 34
  • 109
  • 207
17
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…
Emanuel Landeholm
  • 1,396
  • 1
  • 15
  • 21
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…
Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
15
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 +…
ptomato
  • 56,175
  • 13
  • 112
  • 165
14
votes
5 answers

Is there any case where len(someObj) does not call someObj's __len__ function?

Is there any case where len(someObj) does not call someObj's __len__ function? I recently replaced the former with the latter in a (sucessful) effort to speed up some code. I want to make sure there's not some edge case somewhere where len(someObj)…
David Locke
  • 17,926
  • 9
  • 33
  • 53
14
votes
2 answers

What is the real purpose of magic method __set_state in PHP?

I'm learning about magic methods, reading every site, taking every example, but nothing makes sense to me. Examples like this: class A { public $var1; public $var2; public static function __set_state($an_array) // As of PHP 5.1.0 { …
Red Vulpine
  • 433
  • 5
  • 17
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…
Maxime Fabre
  • 2,252
  • 3
  • 20
  • 24
13
votes
1 answer

Why isn't my class initialized by "def __int__" or "def _init_"? Why do I get a "takes no arguments" TypeError, or an AttributeError?

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 = parameter or: class Example: def…
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
1 2
3
41 42