Magic methods are implicitly invoked by a programming language when some event or language construct is used.
Questions tagged [magic-methods]
545 questions
49
votes
1 answer
How to overload Python's __bool__ method?
Possible Duplicate:
defining “boolness” of a class in python
I thought this should print "False", why is it printing "True"?
>>> class Foo(object):
... def __bool__(self):
... return False
...
>>> f = Foo()
>>> if f:
... print "True"
...…
![](../../users/profiles/573033.webp)
dividebyzero
- 1,153
- 2
- 8
- 16
43
votes
5 answers
Use of PHP Magic Methods __sleep and __wakeup
What is the use of the __sleep and __wakeup magic methods in PHP? I read the PHP documentation but it's still not clear:
class sleepWakeup {
public function __construct() {
// constructor //
}
public function __sleep() {
…
![](../../users/profiles/782535.webp)
Madan Sapkota
- 21,350
- 11
- 100
- 111
35
votes
7 answers
PHP 5.3 Magic Method __invoke
This topic expands on When do/should I use __construct(), __get(), __set(), and __call() in PHP? which talks about the __construct, __get and __set magic methods.
As of PHP 5.3 there is a new Magic Method called __invoke. The __invoke method is…
user83632
35
votes
4 answers
Are Magic Methods Best practice in PHP?
Are Magic Methods Best practice in PHP?
![](../../users/profiles/184814.webp)
OM The Eternity
- 14,004
- 37
- 114
- 174
31
votes
2 answers
Is it possible, using PHPUnit mock objects, to expect a call to a magic __call() method?
I've got a mock object in a test. The real object, PageRepository, implements a magic method using __call(), so if you call $pageRepository->findOneByXXXX($value_of_field_XXXX), it will search the database for records matching that parameter.
Is…
![](../../users/profiles/299589.webp)
Jeremy Warne
- 3,277
- 1
- 27
- 27
31
votes
2 answers
Why does calling Python's 'magic method' not do type conversion like it would for the corresponding operator?
When I subtract a float from an integer (e.g. 1-2.0), Python does implicit type conversion (I think). But when I call what I thought was the same operation using the magic method __sub__, it suddenly does not anymore.
What am I missing here? When I…
![](../../users/profiles/10310883.webp)
doppler
- 912
- 3
- 15
25
votes
2 answers
PHP Child class Magic __isset works but __get doesn't
I have an abstract parent class Mongo_Document (from mongodb-php-odm) and an inherited class Model_ActionPlan. Mongo_Document has magic __isset and __get methods that interact with an array inside the Mongo_Document class.
I am trying to use the…
![](../../users/profiles/829970.webp)
Moshe Katz
- 13,048
- 7
- 58
- 99
24
votes
3 answers
Is there a way to return a custom value for min and max in Python?
I have a custom class,
class A:
def __init__(self, a, b):
self.a = a
self.b = b
The class is not iterable or indexable or anything like that. If at all possible, I would like to keep it that way. Is it possible to have something…
![](../../users/profiles/2988730.webp)
Mad Physicist
- 76,709
- 19
- 122
- 186
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…
![](../../users/profiles/1001985.webp)
McGarnagle
- 96,448
- 30
- 213
- 255
20
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,…
![](../../users/profiles/583780.webp)
abesto
- 2,281
- 15
- 27
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…
![](../../users/profiles/715042.webp)
Michael
- 6,002
- 32
- 55
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…
![](../../users/profiles/83897.webp)
Chad Johnson
- 18,956
- 30
- 98
- 192
17
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?
![](../../users/profiles/7161348.webp)
nik
- 215
- 1
- 7
17
votes
3 answers
Python __index__ special method
>>> class Thing(object):
... def __index__(self):
... return 1
...
>>> thing = Thing()
>>> list_ = ['abc', 'def', 'ghi']
>>> list_[thing]
'def'
>>> dict_ = {1: 'potato'}
>>> dict_[thing]
# KeyError
How does thing know to represent…
![](../../users/profiles/674039.webp)
wim
- 266,989
- 79
- 484
- 630
17
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…
![](../../users/profiles/2883328.webp)
Dennis
- 6,954
- 8
- 53
- 97