magic-methods

Use of PHP Magic Methods __sleep and __wakeup

与世无争的帅哥 提交于 2019-11-28 17:25:50
问题 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() { echo 'Time to sleep.'; } public function __wakeup() { echo 'Time to wakeup.'; } } $ob = new sleepWakeup(); // call __sleep method echo $ob->__sleep(); echo "\n"; // call __wakeup method echo $ob->__wakeup(); This sample code prints: Time to sleep. Time to wakeup. If I were to

I am attempting to print only a selected amount of Pi, it returns with an error of \"Decimal has no attribute: __getitem__

被刻印的时光 ゝ 提交于 2019-11-28 14:53:04
def pi(): prompt=">>> " print "\nWARNING: Pi may take some time to be calculated and may not always be correct beyond 100 digits." print "\nShow Pi to what digit?" n=raw_input(prompt) from decimal import Decimal, localcontext with localcontext() as ctx: ctx.prec = 10000 pi = Decimal(0) for k in range(350): pi += (Decimal(4)/(Decimal(8)*k+1) - Decimal(2)/(Decimal(8)*k+4) - Decimal(1)/(Decimal(8)*k+5) - Decimal(1)/(Decimal(8)*k+6)) / Decimal(16)**k print pi[:int(n)] pi() Traceback (most recent call last): File "/Users/patrickcook/Documents/Pi", line 13, in <module> pi() File "/Users/patrickcook

__get/__set/__call performance questions with PHP

泄露秘密 提交于 2019-11-28 12:25:11
I have a custom-built MVC PHP framework that I am in the process of rewriting and had a question about performance and magic methods. With the model portion of the framework, I was thinking if __get / __set magic methods would cause too much performance hit to be worth using. I mean accessing (reads and writes) model data is going to be one of the most common things performed. Is the use of __get / __set magic methods too big of a performance hit for heavy use functionality like the model portion of a MVC framework? Measure it. It certainly has a big performance hit, especially considering

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

天涯浪子 提交于 2019-11-28 05:44:57
Where is a complete list of the special double-underscore/dunder methods that can be used in classes? (e.g., __init__ , __new__ , __len__ , __add__ ) Please take a look at the special method names section in the Python language reference. Jonny Buchanan Dive Into Python has an excellent appendix for them. If, like me, you want a plain, unadorned list, here it is. I compiled it based on the Python documentation link from the accepted answer. __abs__ __add__ __and__ __call__ __class__ __cmp__ __coerce__ __complex__ __contains__ __del__ __delattr__ __delete__ __delitem__ __delslice__ __dict__ _

How to implement __iadd__ for a Python property

放肆的年华 提交于 2019-11-28 03:08:56
问题 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 + 5 The value of o.x should be the same in the end, so as not to confuse people's expectations, but I want to make the in-place add more efficient. (In reality the operation takes a lot more time than simple addition.) My first idea was to define, in

Why is __getattribute__ not invoked on an implicit __getitem__-invocation?

情到浓时终转凉″ 提交于 2019-11-27 21:52:01
问题 While trying to wrap arbitrary objects, I came across a problem with dictionaries and lists. Investigating, I managed to come up with a simple piece of code whose behaviour I simply do not understand. I hope some of you can tell me what is going on: >>> class Cl(object): # simple class that prints (and suppresses) each attribute lookup ... def __getattribute__(self, name): ... print 'Access:', name ... >>> i = Cl() # instance of class >>> i.test # test that __getattribute__ override works

Using PDO::FETCH_CLASS with Magic Methods

跟風遠走 提交于 2019-11-27 18:09:27
问题 I have a class that uses magic methods to store properties. Here is a simplified example: class Foo { protected $props; public function __construct(array $props = array()) { $this->props = $props; } public function __get($prop) { return $this->props[$prop]; } public function __set($prop, $val) { $this->props[$prop] = $val; } } I'm trying to instantiate objects of this class for each database row of a PDOStatement after it's executed, like this (doesn't work): $st->setFetchMode(PDO::FETCH

How does python numpy.where() work?

只谈情不闲聊 提交于 2019-11-27 17:46:21
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 internally that you are able to pass something like x > 5 into a method? I guess it has something to do with __gt__ but I am looking for a detailed explanation. How do they achieve internally that you are able to pass something like x > 5 into a method? The short answer is that they don't. Any sort of logical operation on a numpy array returns a

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

五迷三道 提交于 2019-11-27 17:39:45
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 are used. Is there a work around to get phpStorm to understand what's going on in magic methods? Our situation is something like this: abstract class a { public static function __callStatic($method,$args) { if(strpos($method,"get_by_") === 0) { //do stuff } elseif(strpos($method,"get_first_by_") === 0) { //do stuff } elseif($method == "get_all") { //do

Which special methods bypasses __getattribute__ in Python?

别说谁变了你拦得住时间么 提交于 2019-11-27 13:45:27
In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the __getattribute__() method even of the object’s metaclass. The docs mention special methods such as __hash__ , __repr__ and __len__ , and I know from experience it also includes __iter__ for Python 2.7. To quote an answer to a related question : "Magic __methods__() are treated specially: They are internally assigned to "slots" in the type data structure to speed up their look-up, and they are only looked up in these slots." In a quest to improve my answer