self

Python decorators in classes

主宰稳场 提交于 2019-11-26 12:43:06
Can one write something like: class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self): pass This fails: self in @self is unknown I also tried: @Test._decorator(self) which also fails: Test unknown I would like to temporarily change some instance variables in the decorator and then run the decorated method, before changing them back. Michael Speer Would something like this do what you need? class Test(object): def _decorator(foo): def magic( self ) : print "start magic" foo( self ) print "end magic" return magic @_decorator def bar( self ) : print "normal call" test

What does new self(); mean in PHP?

China☆狼群 提交于 2019-11-26 11:48:00
问题 I\'ve never seen code like this: public static function getInstance() { if ( ! isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } Is it the same as new className() ? EDIT If the class is inheritant,which class does it point to? 回答1: self points to the class in which it is written. So, if your getInstance method is in a class name MyClass , the following line : self::$_instance = new self(); Will do the same as : self::$_instance = new MyClass(); Edit : a

self.delegate = self; what's wrong in doing that?

て烟熏妆下的殇ゞ 提交于 2019-11-26 11:22:55
问题 self.delegate = self; what\'s wrong in doing that? and what is the correct way of doing it? Thanks, Nir. Code: (UITextField*)initWith:(id)sender:(float)X:(float)Y:(float)width:(float)hieght:(int)textFieldTag { if (self = [super initWithFrame:CGRectMake(X, Y,width, hieght)]) { finalText = [[NSMutableString alloc] initWithString:@\"\"]; senderObject = sender; self.textColor = [UIColor blackColor]; self.font = [UIFont systemFontOfSize:17.0]; self.backgroundColor = [UIColor whiteColor]; self

What is self in ObjC? When should i use it?

北战南征 提交于 2019-11-26 08:30:45
问题 What does self mean in Objective-C? When and where should I use it? Is it similar to this in Java? 回答1: self refers to the instance of the current class that you are working in, and yes, it is analagous to this in Java. You use it if you want to perform an operation on the current instance of that class. For example, if you are writing an instance method on a class, and you want to call a method on that same instance to do something or retrieve some data, you would use self : int value =

How to call an Objective-C Method from a C Method?

醉酒当歌 提交于 2019-11-26 08:10:34
问题 I have an Obj-C object with a bunch of methods inside of it. Sometimes a method needs to call another method inside the same object. I can\'t seem to figure out how to get a C method to call a Obj-C method... WORKS: Obj-C method calling an Obj-C method: [self objCMethod]; WORKS: Obj-C method calling a C method: cMethod(); DOESN\'T WORK: C method calling an Obj-C method: [self objCMethod]; // <--- this does not work The last example causes the compiler spits out this error: error: \'self\'

When to use `self.foo` instead of `foo` in Ruby methods

三世轮回 提交于 2019-11-26 08:10:09
问题 This is not specific for Rails - I am just using Rails as an example. I have a model in Rails: class Item < ActiveRecord::Base def hello puts \"Hello, #{self.name}\" end end (Let\'s say that the Item model (class) has a method called name ). When do I need to use self.name and when can I just use name ? (Eg. #{name} ) 回答1: It is idiomatic to prefer to omit self. when invoking methods; it is generally never needed. You must use self.foo = xxx when calling a setter method, instead of foo = xxx

What is “self” used for in Swift?

天大地大妈咪最大 提交于 2019-11-26 03:40:55
问题 I am new to Swift and I\'m wondering what self is used for and why. I have seen it in classes and structures but I really don\'t find them essential nor necessary to even mention them in my code. What are they used for and why? In what situations it\'s necessary to use it? I have been reading lots of questions and answers for this question but none of them fully answers my questions and they always tend to compare it with this as in Java, with which I\'m not familiar whatsoever. 回答1: You will

How can I decorate an instance method with a decorator class?

一笑奈何 提交于 2019-11-26 03:36:00
问题 Consider this small example: import datetime as dt class Timed(object): def __init__(self, f): self.func = f def __call__(self, *args, **kwargs): start = dt.datetime.now() ret = self.func(*args, **kwargs) time = dt.datetime.now() - start ret[\"time\"] = time return ret class Test(object): def __init__(self): super(Test, self).__init__() @Timed def decorated(self, *args, **kwargs): print(self) print(args) print(kwargs) return dict() def call_deco(self): self.decorated(\"Hello\", world=\"World\

Python decorators in classes

让人想犯罪 __ 提交于 2019-11-26 03:35:02
问题 Can one write something like: class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self): pass This fails: self in @self is unknown I also tried: @Test._decorator(self) which also fails: Test unknown I would like to temporarily change some instance variables in the decorator and then run the decorated method, before changing them back. 回答1: Would something like this do what you need? class Test(object): def _decorator(foo): def magic( self ) : print "start magic" foo(

What is the difference between class and instance variables?

爷,独闯天下 提交于 2019-11-26 03:27:19
What is the difference between class and instance variables in Python? class Complex: a = 1 and class Complex: def __init__(self): self.a = 1 Using the call: x = Complex().a in both cases assigns x to 1. A more in-depth answer about __init__() and self will be appreciated. When you write a class block, you create class attributes (or class variables). All the names you assign in the class block, including methods you define with def become class attributes. After a class instance is created, anything with a reference to the instance can create instance attributes on it. Inside methods, the