self

Is there a generic way for a function to reference itself?

隐身守侯 提交于 2019-11-27 12:40:17
I can access a python function's attribute inside of function itself by below code: def aa(): print aa.__name__ print aa.__hash__ # other simliar However, if above aa() function is a template for write other code, say bb() , I have to write: def bb(): print bb.__name__ print bb.__hash__ # other simliar Is there a "pointer" similar to the self argument in a class method so I could write code like this? def whatever(): print self.__name__ print self.__hash__ # other simliar I searched and found someone said to use the class to solve this problem, but that may be a trouble to redefine all the

Ruby Definition of Self

会有一股神秘感。 提交于 2019-11-27 12:29:47
I was reading a Ruby book and came across this definition of the pseudo-variable self: self - receiver object of the current method Could someone break down that definition and explain what it means? I don't understand any of it. EDIT: I actually have a pretty good idea of what self is (and its applications) and I know how to search on Google. I was just wondering if someone could explain the definition I quoted. That specifically. Ruby and other languages (such as Smalltalk and Objective-C) prefer the term "message passing", whereas Java and C++ prefer "method invocation". That is, the "Java

Why isn't self always needed in ruby / rails / activerecord?

喜你入骨 提交于 2019-11-27 11:33:11
In testing a getter/setter pair in a rails model, I've found a good example of behavior I've always thought was odd and inconsistent. In this example I'm dealing with class Folder < ActiveRecord::Base . Folder belongs_to :parent, :class_name => 'Folder' On the getter method, if I use: def parent_name parent.name end ...or... def parent_name self.parent.name end ...the result is exactly the same, I get the name of the parent folder. However, in the getter method if I use... def parent_name=(name) parent = self.class.find_by_name(name) end ... parent becomes nil, but if I use... def parent_name=

Use of ruby self keyword?

眉间皱痕 提交于 2019-11-27 10:33:27
from what I understand of the self keyword, it simply refers to the current instance of the class. Isn't this the default behaviour at all times anyways? For example, isn't self.var_one = method(args) equivalent to just var_one = method(args) ? If so then what is the use of self? In most cases self.foo is indeed redundant because you can just write foo for the same effect, but in this case it is not and the self is required. var_one = method(args) will create a local variable called var_one , it will not call any method or do anything else to self . self.var_one = method(args) will call the

(Ruby,Rails) Context of SELF in modules and libraries…?

半世苍凉 提交于 2019-11-27 10:31:46
问题 Quick question regarding the use of "SELF" inside a module or library. Basically what is the scope/context of "SELF" as it pertains to a module or library and how is it to be properly used? For an example of what I'm talking about, check out the "AuthenticatedSystem" module installed with "restful_authentication". NOTE: I'm aware that 'self' equates to 'this' in other languages and how 'self' operates on a class/object, however in the context of a module/library there is nothing to 'self'. So

WPF Bind to itself

邮差的信 提交于 2019-11-27 10:13:37
问题 I've got a WPF Window , and somewhere there is a ListView where I bind a List<string> to. Now somewhere in my ListView there is a TextBox and the Content property is set to {Binding} . But this is the shorthand. How do I write the full binding to bind to itself? {Binding Path=Self} doesn't work, neither does {Binding Self} (where the latter is a shortcut for the former). 回答1: Short answer : {Binding} is not a shortcut for "binding to itself" (in the sense of RelativeSource.Self). Rather,

When to use self in Model?

笑着哭i 提交于 2019-11-27 09:55:44
问题 Question: when do I need to use self in my models in Rails? I have a set method in one of my models. class SomeData < ActiveRecord::Base def set_active_flag(val) self.active_flag = val self.save! end end When I do this, everything works fine. However, when I do this: class SomeData < ActiveRecord::Base def set_active_flag(val) active_flag = val save! end end The active_flag value doesn't change, rather it fails silently. Can someone explain? I can't find any duplicates, but if someone finds

Python - self, no self and cls

一曲冷凌霜 提交于 2019-11-27 09:23:37
问题 Yet another question on what the 'self' is for, what happens if you don't use 'self' and what's 'cls' for. I "have done my homework", I just want to make sure I got it all. self - To access an attribute of an object, you need to prefix the attribute name with the object name ( objname.attributename ). The same way self is used to access an attribute inside the object (class) itself. So if you didn't prefix a variable with self in a class method, you wouldn't be able to access that variable in

Why does assigning to self not work, and how to work around the issue?

天大地大妈咪最大 提交于 2019-11-27 08:02:20
问题 I have a class (list of dict s) and I want it to sort itself: class Table(list): … def sort (self, in_col_name): self = Table(sorted(self, key=lambda x: x[in_col_name])) but it doesn't work at all. Why? How to avoid it? Except for sorting it externally, like: new_table = Table(sorted(old_table, key=lambda x: x['col_name']) Isn't it possible to manipulate the object itself? It's more meaningful to have: class Table(list): pass than: class Table(object): l = [] … def sort (self, in_col_name):

In Go is naming the receiver variable 'self' misleading or good practice?

妖精的绣舞 提交于 2019-11-27 07:04:52
I have seen a fair amount of blogs & videos on Go and as far as I recall, none of the authors use 'self' or 'this' for the receiver variable when writing methods. However there seems to be a number of questions on stack overflow that do this, and it got me thinking about if this is misleading to name the variable 'self'? Reading the Spec for Method Sets does not provide any evidence either way (in my interpretation). I seem to recall finding somewhere that it was not really a self pointer, can anyone list evidence or provide reasoning either way, and if any problems/traps that might occur from