Questions tagged [magic-methods]

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

545 questions
3037
votes
24 answers

What is the difference between __str__ and __repr__?

What is the difference between __str__ and __repr__ in Python?
Casebash
  • 100,511
  • 79
  • 236
  • 337
167
votes
14 answers

Python __call__ special method practical example

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 and perform the same operation done in __call__ method…
mohi666
  • 5,904
  • 8
  • 34
  • 44
124
votes
9 answers

Best practice: PHP Magic Methods __set and __get

Possible Duplicate: Are Magic Methods Best practice in PHP? These are simple examples, but imagine you have more properties than two in your class. What would be best practice? a) Using __get and __set class MyClass { private $firstField; …
rfc1484
  • 8,473
  • 15
  • 58
  • 111
107
votes
4 answers

Simple example of use of __setstate__ and __getstate__

I don't know what the __setstate__ and __getstate__ methods do, so help me with a simple example.
zjm1126
  • 52,371
  • 71
  • 163
  • 213
101
votes
7 answers

Why does Python use 'magic methods'?

I've been playing around with Python recently, and one thing I'm finding a bit odd is the extensive use of 'magic methods', e.g. to make its length available, an object implements a method, def __len__(self), and then it is called when you write…
Greg Beech
  • 122,952
  • 42
  • 199
  • 241
96
votes
3 answers

How does python numpy.where() work?

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.where( x > 5 ) (array([2, 2, 2]), array([0, 1, 2])) How do they achieve…
pajton
  • 14,670
  • 6
  • 50
  • 63
96
votes
3 answers

What is the __dict__.__dict__ attribute of a Python class?

>>> class A(object): pass ... >>> A.__dict__ >>> A.__dict__.__dict__ Traceback (most recent call last): File "", line 1, in AttributeError: 'dictproxy' object has no attribute '__dict__' >>>…
porgarmingduod
  • 7,070
  • 7
  • 41
  • 76
91
votes
4 answers

Making a python user-defined class sortable, hashable

What methods need to be overridden/implemented when making user-defined classes sortable and/or hashable in python? What are the gotchas to watch out for? I type dir({}) into my interpreter to get a list of methods on built-in dicts. Of those, I…
Matt Fenwick
  • 44,546
  • 19
  • 115
  • 184
89
votes
2 answers

scala slick method I can not understand so far

I try to understand some Slick works and what it requires. Here it an example: package models case class Bar(id: Option[Int] = None, name: String) object Bars extends Table[Bar]("bar") { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) //…
ses
  • 12,339
  • 25
  • 106
  • 203
87
votes
8 answers

PHP __get and __set magic methods

Unless I'm completely mistaken, the __get and __set methods are supposed to allow overloading of the → get and set. For example, the following statements should invoke the __get method: echo $foo->bar; $var = $foo->bar; And the following should use…
airbear
  • 889
  • 1
  • 7
  • 4
86
votes
2 answers

How to document magic (_call and _callStatic) methods for IDEs

After many happy years coding in notepad++ and sublime, I've been advised to give a PHP IDE a go. I'm trying out phpStorm and it seems nice. The code completion and documentation is a great feature but isn't working out for me when magic methods…
Rob Forrest
  • 6,682
  • 7
  • 48
  • 67
84
votes
11 answers

Is it possible to overload Python assignment?

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): raise Exception("This is an ex-parrot") var =…
Caruccio
  • 2,679
  • 3
  • 16
  • 12
79
votes
3 answers

How does Scala's apply() method magic work?

In Scala, if I define a method called apply in a class or a top-level object, that method will be called whenever I append a pair a parentheses to an instance of that class, and put the appropriate arguments for apply() in between them. For…
Jeff
  • 12,441
  • 15
  • 46
  • 58
62
votes
8 answers

PHP - Indirect modification of overloaded property

I know this question has been asked several times, but none of them have a real answer for a workaround. Maybe there's one for my specific case. I'm building a mapper class which uses the magic method __get() to lazy load other objects. It looks…
w00
  • 23,892
  • 26
  • 94
  • 145
50
votes
9 answers

Where is the Python documentation for the special methods? (__init__, __new__, __len__, ...)

Where is a complete list of the special double-underscore/dunder methods that can be used in classes? (e.g., __init__, __new__, __len__, __add__)
mk12
  • 24,644
  • 29
  • 92
  • 132
1
2 3
36 37