Questions tagged [magic-function]

  1. Python magic functions

    Magic methods in Python are the methods having two prefix and suffix underscores in the method name. These are commonly used for operator overloading. Built-in classes in Python define many magic methods. Few examples for magic methods are: __init__, __add__, __len__, __repr__ etc. Use the dir() function to see the number of magic methods inherited by a class. For example, the following lists all the attributes and methods defined in the int class.

    >>> dir(int)
    

    ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

    When we add two numbers using the + operator. the __add__ method is a magic method which gets called.

  2. PHP magic functions

    Magic methods in PHP are named with double underscore (__) as prefix. All these function names are reserved and can't be used for any purpose other than associated magical functionality. Magical method in a class must be declared public. Few examples for magic methods are:

    __construct()
    __destruct()
    __call($fun, $arg)
    __callStatic($fun, $arg)
    __get($property)
    __set($property, $value)
    __isset($content)
    __unset($content)
    __sleep()
    __wakeup()
    __toString()
    __invoke()
    __set_state($array)
    __clone()
    __debugInfo()
    
45 questions
152
votes
5 answers

What is %timeit in Python?

I always read the code to calculate the time like this way: %timeit function() What does "%" mean here? I think, the "%" is always used to replace something in a string, like %s means replace a string, %d replace a data, but I have no idea about…
xirururu
  • 5,028
  • 9
  • 35
  • 64
47
votes
2 answers

Interactive Python: cannot get `%lprun` to work, although line_profiler is imported properly

Problem Most iPython "magic functions" work fine for me right off the bat: %hist, %time, %prun, etc. However, I noticed that %lprun could not be found with iPython as I'd installed it originally. Attempt to Resolve I then discovered that I should…
Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
11
votes
10 answers

When do/should I use __construct(), __get(), __set(), and __call() in PHP?

A similar question discusses __construct, but I left it in my title for people searching who find this one. Apparently, __get and __set take a parameter that is the variable being gotten or set. However, you have to know the variable name (eg, know…
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
7
votes
2 answers

In IPython how do I create aliases for %magics?

Let's say I want to create the alias %xed for %edit -x. How would I do it?
Redwood
  • 66,744
  • 41
  • 126
  • 187
7
votes
4 answers

What's the magic function for empty() in PHP?

It should not be __isset,because isset() is not the same thing as empty()
user198729
  • 61,774
  • 108
  • 250
  • 348
6
votes
2 answers

Mercurial/Python - What Does The Underscore Function Do?

In Mercurial, many of the extensions wrap their help/syntax string in a call to an underscore function, like so: _('[OPTION] [QUEUE]') This confuses me, because it does not seem necessary (the Writing Extensions instructions don't mention it) and…
dimo414
  • 47,227
  • 18
  • 148
  • 244
5
votes
1 answer

Python overloading variable assignment

I have a class definition like class A(object): def __init__(self): self.content = u'' self.checksum = hashlib.md5(self.content.encode('utf-8')) Now when I change the self.content, I want that self.checksum would automatically…
Dewsworld
  • 13,367
  • 23
  • 68
  • 104
4
votes
2 answers

%matplotlib inline gives the same result with or without it on Jupyter Notebook

So I went through some questions being posted about the usage of %matplotlib inline function in Jupyter Notebook, I do understand that "%matplotlib inline sets the backend of matplotlib to the 'inline' backend" & "When using the 'inline' backend,…
SMAmir
  • 75
  • 1
  • 12
3
votes
1 answer

How to map GROUP_CONCAT with pdo->fetchAll and mode PDO::FETCH_CLASS from string to an array

I was wondering how fetchAll of PDO is actually implemented to get an Idea how to map the result from the database including a GROUP_CONCAT() comma separated list string to an array property. Having a sql like $query = "Select a.id,…
helle
  • 11,183
  • 9
  • 56
  • 83
3
votes
5 answers

Python newbie clarification about tuples and strings

I just learned that I can check if a substring is inside a string using: substring in string It looks to me that a string is just a special kind of tuple where its elements are chars. So I wonder if there's a straightforward way to search a slice…
Yuta73
  • 188
  • 10
3
votes
2 answers

Magic function timeit

I'm using the magic %%timeit function to get the time it takes to execute some code. The thing that bothers me is that when I run %%timeit, I don't get the results. For instace: a=5 b=3 %%timeit c = a + b Now if I want to use c in the next cell, I…
MarcoQ
  • 33
  • 1
  • 3
3
votes
1 answer

Passing a variable to __wakeup()

I am storing certain objects in the database. Each node in the database carries a serialized object. The node can have children, and thus, the object can have children too. Therefore, i want to fill a $children property in the object with an array…
SquareCat
  • 5,699
  • 9
  • 41
  • 75
3
votes
1 answer

Getting data from a Magento Collection

I have a collection that has one row of data in it. If I do following, $collection->getData(); it give me an array like below, array(1) { [0] => array(3) { ["id"] => string(1) "1" ["field1"] => string(10) "Field 1 Data" …
Thanu
  • 2,481
  • 8
  • 34
  • 53
3
votes
1 answer

Magic functions and inheritance

I would like to know if it's possible to create a magic object that extends another magic object, (with PHP).
Roch
  • 21,741
  • 29
  • 77
  • 120
2
votes
1 answer

Using __get() on array class properties to auto-propogate with a class

I have a activerecord model which has an array in it of say: "$sessions". What I would like to do is make this a 2d array so as to be indexed in the following way: ["0"=>Session(Object), "1"=>Session(Object)] Now to add cream on top I would like to…
Sammaye
  • 43,242
  • 7
  • 104
  • 146
1
2 3