self

Python assignment to self in constructor does not make object the same

梦想与她 提交于 2019-11-29 11:23:40
I am making a constructor in Python. When called with an existing object as its input, it should set the "new" object to that same object. Here is a 10 line demonstration: class A: def __init__(self, value): if isinstance(value, A): self = value else: self.attribute = value a = A(1) b = A(a)#a and b should be references to the same object print("b is a", b is a)#this should be true: the identities should be the same print("b == a", b == a)#this should be true: the values should be the same I want the object A(a) constructed from the existing object a to be a . Why is it not? To be clear, I

class, dict, self, init, args?

ぐ巨炮叔叔 提交于 2019-11-29 07:59:29
问题 class attrdict(dict): def __init__(self, *args, **kwargs): dict.__init__(self, *args, **kwargs) self.__dict__ = self a = attrdict(x=1, y=2) print a.x, a.y b = attrdict() b.x, b.y = 1, 2 print b.x, b.y Could somebody explain the first four lines in words? I read about classes and methods. But here it seems very confusing. 回答1: You are not using positional arguments in your example. So the relevant code is: class attrdict(dict): def __init__(self, **kwargs): dict.__init__(self, **kwargs) self._

Why do I get the error “Protocol … can only be used as a generic constraint because it has Self or associated type requirements”?

我们两清 提交于 2019-11-29 07:47:49
I wrote an extension onto Int as below. extension Int { func squared () -> Int { return self * self } } print(10.squared()) // works The above code works. Now I want to extend the IntegerType protocol so that Int, UInt, Int64, etc would all conform. My code is as below. extension IntegerType { func squared () -> IntegerType { // this line creates error return self * self } } I get error: Protocol 'IntegerType' can only be used as a generic constraint because it has Self or associated type requirements I already saw this question and its video & this question, still couldn't understand. I only

Can a JavaScript function return itself?

吃可爱长大的小学妹 提交于 2019-11-29 05:27:43
Can I write a function that returns iteself? I was reading some description on closures - see Example 6 - where a function was returning a function, so you could call func()(); as valid JavaScript. So I was wondering could a function return itself in such a way that you could chain it to itself indefinitely like this: func(arg)(other_arg)()(blah); Using arguments object, callee or caller? There are 2-3 ways. One is, as you say, is to use arguments.callee . It might be the only way if you're dealing with an anonymous function that's not stored assigned to a variable somewhere (that you know of)

Difference between Python self and Java this

旧时模样 提交于 2019-11-28 19:33:03
问题 I had done a bit of Python long back. I am however moving over to Java now. I wanted to know if there were any differences between the Python "self" method and Java "this". I know that "self" is not a keyword while "this" is. And that is pretty much what I could figure out. Am I missing anything else? 回答1: About self in Python (here is the source: Python self explanation): The reason you need to use self . is because Python does not use the @ syntax to refer to instance attributes. Python

In Ruby, when should you use self. in your classes? [duplicate]

女生的网名这么多〃 提交于 2019-11-28 17:46:55
问题 This question already has answers here : When to use `self.foo` instead of `foo` in Ruby methods (3 answers) Closed 6 years ago . When do you use self.property_name in Ruby? 回答1: Use self when calling a class's mutator. For example, this won't work: class Foo attr_writer :bar def do_something bar = 2 end end The problem is that 'bar = 2' creates a local variable named 'bar', rather than calling the method 'bar=' which was created by attr_writer. However, a little self will fix it: class Foo

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

瘦欲@ 提交于 2019-11-28 17:31:24
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 then what is the context of self inside something like a module where there is no class? In a module:

WPF Bind to itself

大城市里の小女人 提交于 2019-11-28 17:08:20
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). Short answer : {Binding} is not a shortcut for "binding to itself" (in the sense of RelativeSource.Self ). Rather, {Binding} is equivalent to {Binding Path=.} , which binds to the current source. To elaborate : A binding has a

When to use self in Model?

强颜欢笑 提交于 2019-11-28 16:47:34
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 one that's fine too. DVG When you're doing an action on the instance that's calling the method, you use

Python - self, no self and cls

蹲街弑〆低调 提交于 2019-11-28 15:58:00
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 other methods of the class, or outside of the class. So you could omit it if you wanted to make the