magic-methods

Increment on “__toString”

人盡茶涼 提交于 2019-12-04 03:55:35
I am not sure what the title should be, but the code should explain it better: class Group { private $number = 20; public function __toString() { return "$this->number"; } } $number = new Group(); echo $number, PHP_EOL; echo ++ $number, PHP_EOL; echo PHP_EOL; $number = "20"; echo $number, PHP_EOL; echo ++ $number, PHP_EOL; echo PHP_EOL; $number = 20; echo $number, PHP_EOL; echo ++ $number, PHP_EOL; Output: 20 20 <--- Expected 21 20 21 20 21 Any idea why I got 20 instead of 21 ? Even then the code below works: $i = null ; echo ++$i ; // output 1 I know Group is an object that implements _

Built-in magic variable names/attributes

末鹿安然 提交于 2019-12-04 03:19:00
Background : For those not familiar with it, Sublime Text (and TextMate) provides syntax highlighting and other features through scopes which are defined by .tmLanguage language definition files, basically a bunch of regexes to identify various constructs in a given language, such as function definitions, various types of strings, reserved words, etc. I'm the maintainer of the Python Improved package (available via Package Control if you're interested) that aims to be a better language definition for Python. You can read about it at GitHub if you want, but one of the key features is that it's

Magic Methods in JavaScript [duplicate]

主宰稳场 提交于 2019-12-04 02:53:58
This question already has answers here : Closed 5 years ago . JavaScript getter for all properties (7 answers) Are the any magic(al) methods under JavaScript ? I'm explicitly interested in setters , getters and callables (functions/methods), like those we have in PHP : __set , __get and __call && __callStatic . I was googling and searching StackOverflow, but no results. Please, if anyone is informed - THANKS! Tomasz Kowalczyk see this: JavaScript's equivalent to PHP's __get() magic method JavaScript getter for all properties 来源: https://stackoverflow.com/questions/6303896/magic-methods-in

PHP - Zend say avoid Magic Methods?

好久不见. 提交于 2019-12-03 12:59:54
I was reading this page - http://deaduseful.com/blog/posts/50-php-optimisation-tips-revisited And one of the recommendations was to avoid using Magic Methods, cited from a Zend Performance PDF which gives no reason for its recommendation to avoid them. After some Google searching (and winding up here to an unrelated question) I wondered if anyone had any reccomendations on that front? I use __get() alot in my code, usually to save variables that I don't always use e.g. I may have a table with name, desc, category_id, time_added My get would look something like this: public function __get($name

PHP magic methods example

旧街凉风 提交于 2019-12-03 12:15:07
I have this question from the Zend PHP study guide and can't find a proper explanation... <?php class Magic { public $a = "A"; protected $b = array("a"=>"A", "b"=>"B", "c"=>"C"); protected $c = array(1,2,3); public function __get($v) { echo "$v,"; return $this->b[$v]; } public function __set($var, $val) { echo "$var: $val,"; $this->$var = $val; } } $m = new Magic(); echo $m->a.",".$m->b.",".$m->c.","; $m->c = "CC"; echo $m->a.",".$m->b.",".$m->c; ?> According to the guide, solution shall be " b,c,A,B,C,c: CC,b,c,A,B,C ". I can't figure out why - maybe you do? My intention is that the first

Custom double star operator for a class?

元气小坏坏 提交于 2019-12-03 12:01:41
How does one implement custom double star operator ( ** ) for unpacking, similar to how __iter__ works with single star operator ( * )? For example: class PlayerManager(object): def __init__(self, players=None): self.players = players or [] # Made up method to support ** operator def __dict_iter__(self): for player in self.players: yield get_steamid(player), player def print_players(**players): print(players) player_manager = PlayerManager([list, of, players]) print_players(**player_manager) Output: { 'STEAM_0:0:02201': <Player object at 0x0000000000>, 'STEAM_0:0:10232': <Player object at

How to write a static python getitem method?

落爺英雄遲暮 提交于 2019-12-03 11:05:49
What do I need to change to make this work? class A: @staticmethod def __getitem__(val): return "It works" print A[0] Note that I am calling the __getitem__ method on the type A . When an object is indexed, the special method __getitem__ is looked for first in the object's class. A class itself is an object, and the class of a class is usually type . So to override __getitem__ for a class, you can redefine its metaclass (to make it a subclass of type ): class MetaA(type): def __getitem__(cls,val): return "It works" class A(object): __metaclass__=MetaA pass print(A[0]) # It works In Python3 the

How to overload Python's __bool__ method? [duplicate]

本秂侑毒 提交于 2019-12-03 05:25:41
问题 This question already has answers here : Closed 7 years ago . 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" ... else: ... print "False" ... True >>> 回答1: You should define __nonzero__() in Python 2.x. It was only renamed to __bool__() in Python 3.x. (The name __nonzero__() actually predates the introduction

Is there a way to return a custom value for min and max in Python?

我的未来我决定 提交于 2019-12-03 05:07:24
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 like the following work? >>> x = A(1, 2) >>> min(x) 1 >>> max(x) 2 What got me thinking about this is that min and max are listed as "Common Sequence Operations" in the docs . Since range is considered to be a sequence type by the very same docs, I was thinking that there must be some sort of optimization that is possible for range , and that perhaps I could take

Python class method chaining

烂漫一生 提交于 2019-12-03 03:21:23
To avoid getting lost in architectural decisions, I'll ask this with an analogous example: lets say I wanted a Python class pattern like this: queue = TaskQueue(broker_conn) queue.region("DFW").task(fn, "some arg") The question here is how do I get a design a class such that certain methods can be "chained" in this fashion. task() would require access to the queue class instance attributes and the operations of task depends on the output of region() . I see SQLalchemy does this (see below) but am having difficulty digging through their code and isolating this pattern. query = db.query(Task)