self

PHP Calling self on a non-static method

南楼画角 提交于 2019-12-03 14:31:01
Why is the 'self'-call to a non-satic method in this example working? class A{ protected function aNonStaticMethod(){ return __class__; } public function aEcho(){ echo self::aNonStaticMethod(); } } Thanks for explanation. Calling non-static method statically Theoretically it should not work, but as this comment says: There was no static keyword in php4 but php4 did allow for static calls. To maintain backwards compatibility this was left in when the static keyword was added in php5. This comment is supported by this official php.net wiki: This is already deprecated if the call occurs from an

When to use self in module's methods

[亡魂溺海] 提交于 2019-12-03 12:38:20
My module definition looks like this: module RG::Stats def self.sum(a, args = {}) a.inject(0){ |accum, i| accum + i } end end To use this method I simply require the file containing this definition so that I can do: RG::Stats.sum(array) and also RG::Stats.method(:sum) However, if I need to know the list of methods using RG::Stats.instance_methods I get an empty array. This is because I have used self . If I omit self then RG::Stats.instance_methods gives the list of methods, but I cannot access them anymore. The question is: how to use self in module's method definition? izaban Use self in

Why use [ClassName alloc] instead of [[self class] alloc]?

拥有回忆 提交于 2019-12-03 10:10:33
I'm reading through Mark Dalrymple's Learn Objective-C on the Mac (only at the chapter on Protocols, so still relatively newbish) and trying to figure something out: Why would you ever reference a class by its own name? If I had a class called Foo , why would I ever want to write, say, [[Foo alloc] init] and not [[[self class] alloc] init] If I had a subclass Bar, wouldn't the first option invalidate me from writing [[Bar alloc] init] whereas the second option would allow it? When would the first option be better? Generally, within a class method, you do use [[self alloc] init] . For example,

Python calling method without 'self'

≡放荡痞女 提交于 2019-12-03 04:21:18
So I just started programming in python and I don't understand the whole reasoning behind 'self'. I understand that it is used almost like a global variable, so that data can be passed between different methods in the class. I don't understand why you need to use it when your calling another method in the same class. If I am already in that class, why do I have to tell it?? example, if I have: Why do I need self.thing()? class bla: def hello(self): self.thing() def thing(self): print "hello" Also you can make methods in class static so no need for self . However, use this if you really need

Ruby self keyword

吃可爱长大的小学妹 提交于 2019-12-03 03:58:36
im having trouble understanding the self keyword . I get how it's used to distinguish between Instance Methods and Class Methods but what about when it's used from inside a method. Something like def self.name self.name = "TEXT" end or def name2 self.name = "TEXT2" end or class Array def iterate!(&code) self.each_with_index do |n, i| self[i] = code.call(n) end end end Usually, self as a receiver can be omitted, and in such cases, it is usually preferable to do so. However, there are a few cases when omitting self make the code mean something else. One such case is, as in your example self.name

When to access property with self and when not to?

寵の児 提交于 2019-12-03 03:38:32
Can anyone explain the difference between setting someObject = someOtherObject; and self.someObject = someOtherObject; if someObject is a class property created with @property (nonatomic, retain) SomeType someObject; To clarify I have something like: @interface SomeClass : NSObject { SomeType* someObject; } @property (nonatomic, retain) SomeType* someObject; @end I have noticed I get EXC_BAD ACCESS sometimes when I use the property without self and it seems quite random. When I use self my program acts as it should be. I don’t get any compiler errors or warnings when I skip self so I guess it

Rails — self vs. @

老子叫甜甜 提交于 2019-12-03 03:18:18
问题 I am following Michael Hartl's RoR tutorial, and it is covering the basics of password encryption. This is the User model as it currently stands: class User < ActiveRecord::Base attr_accessor :password attr_accessible :name, :email,: password, :password_confirmation email_regex = /^[A-Za-z0-9._+-]+@[A-Za-z0-9._-]+\.[A-Za-z0-9._-]+[A-Za-z]$/ #tests for valid email addresses. validates :name, :presence => true, :length => {:maximum => 50} validates :email, :presence => true, :format => {:with =

Python - Is it okay to pass self to an external function

蓝咒 提交于 2019-12-02 23:25:44
I have a class, A, which is inherited by a bunch of other classes. Some of these have a few functions which are similar and it would be nice to have those functions defined somewhere else and called by the classes that need them. But those functions call functions defined in the super class. class A(): def imp_func(*args): # called by the child class functions Class B(A): def common_func(self): # some stuff self.imp_func(*args) So I have created my helper functions which take the self object as an argument and I can call the imp_func from inside the helper functions. def helper_func(obj, some

What is the purpose of checking self.__class__ ? - python

孤街浪徒 提交于 2019-12-02 23:00:21
What is the purpose of checking self.__class__ ? I've found some code that creates an abstract interface class and then checks whether its self.__class__ is itself, e.g. class abstract1 (object): def __init__(self): if self.__class__ == abstract1: raise NotImplementedError("Interfaces can't be instantiated") What is the purpose of that? Is it to check whether the class is a type of itself? The code is from NLTK's http://nltk.googlecode.com/svn/trunk/doc/api/nltk.probability-pysrc.html#ProbDistI self.__class__ is a reference to the type of the current instance. For instances of abstract1 , that

Python- how to get list of self variables in a class consist of N-self

允我心安 提交于 2019-12-02 22:37:32
问题 Edited: I want to generate N-number of agents. Each agent will have a name, so I create a random name from names and assigned it to class Agent. After I run the model, I want to get the list of my agents name. This is from mesa: import names from mesa import Agent, Model from mesa.time import RandomActivation class Agent(Agent): def __init__(self, name): self.name= names.get_full_name() self.wealth = 1 def step(self): pass class Model(Model): def __init__(self, N): self.num_agents = N self