Questions tagged [magic-methods]

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

616 questions
3635
votes
28 answers

What is the difference between __str__ and __repr__?

What is the difference between __str__ and __repr__ in Python?
Casebash
  • 114,675
  • 90
  • 247
  • 350
281
votes
11 answers

Understanding __getitem__ method in Python

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 implement calls like self[key]. But what is the use of…
Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
177
votes
16 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
  • 6,842
  • 9
  • 45
  • 51
146
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
  • 63,397
  • 81
  • 173
  • 221
130
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
  • 48,199
  • 22
  • 128
  • 192
130
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
  • 9,441
  • 16
  • 72
  • 123
111
votes
13 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
  • 3,489
  • 4
  • 18
  • 14
110
votes
7 answers

Why does Python use 'magic methods'?

I'm a bit surprised by Python's extensive use of 'magic methods'. For example, in order for a class to declare that instances have a "length", it implements a __len__ method, which it is called when you write len(obj). Why not just define a len…
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
104
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,668
  • 10
  • 50
  • 83
101
votes
4 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
  • 15,828
  • 8
  • 54
  • 65
95
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
  • 969
  • 1
  • 7
  • 4
94
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
  • 7,329
  • 7
  • 52
  • 69
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
  • 13,174
  • 31
  • 123
  • 226
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
  • 14,831
  • 15
  • 49
  • 59
78
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
  • 26,172
  • 30
  • 101
  • 147
1
2 3
41 42