self-reference

Self referential enum with immutable parameters

旧巷老猫 提交于 2019-12-21 09:17:27
问题 Consider the following sscce public enum Flippable A (Z), B (Y), Y (B), Z (A); private final Flippable opposite; private Flippable(Flippable opposite) { this.opposite = opposite; } public Flippable flip() { return opposite; } } This doesn't compile, because Z and Y haven't been declared to be allowed to be arguments of A and B 's constructor. Potential solution 1: Hardcoded Methods public enum Flippable { A { public Flippable flip() { return Z; } }, B { public Flippable flip() { return Y; } }

Python: getting a reference to a function from inside itself

点点圈 提交于 2019-12-21 03:19:13
问题 If I define a function: def f(x): return x+3 I can later store objects as attributes of the function, like so: f.thing="hello!" I would like to do this from inside the code of the function itself. Problem is, how do I get a reference to the function from inside itself? 回答1: The same way, just use its name. >>> def g(x): ... g.r = 4 ... >>> g <function g at 0x0100AD68> >>> g(3) >>> g.r 4 回答2: If you are trying to do memoization, you can use a dictionary as a default parameter: def f(x, memo={}

iterative cumsum where sum determines the next position to be added

谁说我不能喝 提交于 2019-12-20 07:46:47
问题 I have a data.table as follows set.seed(5) x <- data.table(x=sample(1:20,15)) > x x 1: 5 2: 14 3: 17 4: 20 5: 2 6: 11 7: 8 8: 15 9: 12 10: 16 11: 3 12: 18 13: 10 14: 4 15: 13 and I would like to start at 1 and cumulate values iteratively such that the value of cumsum() determines the next number to be added to the sum. In the example I want to add the first value of x , here 5, then jump to value number 5 and add that, here 2, then jump to value number 5+2=7 , here 8, then value number 5+2+8

how to build self referencing table

耗尽温柔 提交于 2019-12-20 04:55:45
问题 In the source table, there are two columns as following snapshot shows: Then for destination table, it should be something like this: ( "DimLocationKey" is auto-generated surrogate key ) How could I achieve self-referencing effect in SSIS? I tried following approach but it's not working because there would be no matches in the lookup. 回答1: If the column is nullable, then you could load the unique values for location_ID and then have a secondary process come back through and take care of

Entity Framework Code First Self Referencing Parent Child with Payload

故事扮演 提交于 2019-12-18 08:29:44
问题 I'm attempting to set this up using code first in entity framework and am running into difficulty. To describe what i'm trying to accomplish: Have an entity of Product. This product optionally may have one or more related "child" products. A product can be the child to one or more parent products. when I go to generate a controller tied to the model class "Product", i'm getting an error: (updated, more specific, matches code below) There was an error running the selected code generator:

Javascript literal object, reference to itself

↘锁芯ラ 提交于 2019-12-18 04:13:01
问题 I have this example code: var foo = { self: this, init: function(){ self.doStuff(); }, doStuff: function(){ alert('doing stuff'); } } foo.init(); Why the refence "self" doesn't work? Thanks! 回答1: Following up on Qeuntin's response you would use the following to achieve what you're looking for var foo = { self: false, init: function(){ self = this self.doStuff(); }, doStuff: function(){ alert('doing stuff'); }, } EDIT: Since it's been pointed out that whilst this solves OP's problem (i.e it

Entity Framework 4 CTP 5 Self Referencing Many-to-Many

爷,独闯天下 提交于 2019-12-18 02:49:38
问题 I have the following scenario in my database. It is a record of Studies and those studies have other studies as prerequisites. In my DB design, it looks like this: And my code looks something like this: public class Study { public int ID { get; set; } public string Topic { get; set; } public byte TypeID { get; set; } public virtual StudyType Type { get; set; } public bool Deprecated { get; set; } public virtual ICollection<Study> Prerequisites { get; set; } } public class StudyType { public

How to get a reference to a module inside the module itself?

不问归期 提交于 2019-12-17 17:29:01
问题 How can I get a reference to a module from within that module? Also, how can I get a reference to the package containing that module? 回答1: import sys current_module = sys.modules[__name__] 回答2: If you have a class in that module, then the __module__ property of the class is the module name of the class. Thus you can access the module via sys.modules[klass.__module__] . This is also works for functions. 回答3: One more technique, which doesn't import the sys module, and arguably - depends on

How can I refer to the class type a interface is implementing in Java?

↘锁芯ラ 提交于 2019-12-17 15:46:09
问题 I came to a problem with interfaces in a program I'm making. I want to create a interface which have one of its methods receiving/returning a reference to the type of the own object. It was something like: public interface I { ? getSelf(); } public class A implements I { A getSelf() { return this; } } public class B implements I { B getSelf() { return this; } } I can't use an "I" where it's a "?", because I don't want to return a reference to the interface, but the class. I searched and found

How can I achieve a self-referencing many-to-many relationship on the SQLAlchemy ORM back referencing to the same attribute?

痞子三分冷 提交于 2019-12-17 15:34:48
问题 I'm trying to implement a self-referential many-to-many relationship using declarative on SQLAlchemy. The relationship represents friendship between two users. Online I've found (both in the documentation and Google) how to make a self-referential m2m relationship where somehow the roles are differentiated. This means that in this m2m relationships UserA is, for example, UserB's boss, so he lists him under a 'subordinates' attribute or what have you. In the same way UserB lists UserA under