self

Ruby: How to write a bang method, like map?

爱⌒轻易说出口 提交于 2019-12-30 04:48:05
问题 I'd like to write some new Array methods that alter the calling object, like so: a = [1,2,3,4] a.map!{|e| e+1} a = [2,3,4,5] ...but I'm blanking on how to do this. I think I need a new brain. So, I'd like something like this: class Array def stuff! # change the calling object in some way end end map! is just an example, I'd like to write a completely fresh one without using any pre-existing ! methods. Thanks! 回答1: EDIT - Updated answer to reflect the changes to your question. class Array def

php self() with current object's constructor

£可爱£侵袭症+ 提交于 2019-12-25 05:04:55
问题 What's the proper way to get new self() to use the current instance's constructor? In other words, when I do: class Foo{ function create(){ return new self(); } } Class Bar extends Foo{ } $b = new Bar(); echo get_class($b->create()); I want to see: Bar instead of: Foo 回答1: public static function create() { $class = get_called_class(); return new $class(); } This should work. class Foo{ public static function create() { $class = get_called_class(); return new $class(); } } class Bar extends

Video, flash or… videos for HTML [closed]

我与影子孤独终老i 提交于 2019-12-25 02:28:39
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I'm not new to HTML, CSS, JS or PHP, I already made diff. web pages, but every time when I included a video from YouTube or from another external site, I always used iframe, or embed code. So, today I started to

Referencing `self` in `__old__` in PyContract constraints

跟風遠走 提交于 2019-12-25 02:19:37
问题 I'm working on writing some constraints for a class method using PyContract (not PyContracts). As a postcondition, I'd like to ensure that the memory address of the instance hasn't changed i.e. id(self) should be the same before and after calling the function. How can I do this with PyContract? I have the following (minimal) code: class Individual: def append(self, chrom): """ post: __old__.self is self len(__old__.self.chromosomes)+1 == len(self.chromosomes) self.chromosomes[-1] == chrom """

When can I omit self

吃可爱长大的小学妹 提交于 2019-12-25 01:07:18
问题 I found this example of a simple tkinter class. The example works with either 'frame' or 'self.frame' Why can or would I safely omit 'self'? from Tkinter import * class App: def __init__(self, master): frame = Frame(master) frame.pack() self.button = Button( frame, text="QUIT", fg="red", command=frame.quit ) self.button.pack(side=LEFT) self.hi_there = Button(frame, text="Hello", command=self.say_hi) self.hi_there.pack(side=LEFT) def say_hi(self): print "hi there, everyone!" root = Tk() app =

Delphi - Maintaining Self References to an Object

随声附和 提交于 2019-12-24 16:53:27
问题 I've looked at many questions and resources which deal with the "Self" variable in an Object, but everyone says something different. For example, in this question: Delphi Self-Pointer usage the highest rated answer to the question appears to be wrong. Pointer(Self) does not point to the object which contains it, and cannot be used to pass references from inside the object. I've tried doing this: Type myobject = class PSelf: Pointer; End; Var Obj: myobject; Procedure RunProgram; Begin Obj :=

Refering to the class itself from within a class mehod in Objective C

泄露秘密 提交于 2019-12-24 02:39:13
问题 I hope i did not skip this part in the ObjC manual but is it possible to refer to a class from within one of its class methods? Like in PHP you would use "this" to refer to the current instance, while "self" refers to the instance's class the ObjC equivalent of "this" would be "self", so what would be the ObjC equivalent of PHP's "self", if there is one? 回答1: Inside a class method, self refers to the current class (the class's Class object). Inside an instance method, self refers to the

PHP condition, if current page, then link is highlighted [closed]

跟風遠走 提交于 2019-12-24 00:45:19
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I have a webpage with a side bar that uses some css class. If the class = "active', then the link will be highlighted. I want to make it so that at any

How to get self object name from self method in Python

假如想象 提交于 2019-12-23 17:16:20
问题 I am trying to find a way to automatically print the object reference name with just a print object To be more specific. Lets say I have a class: class A: def __init__(self): self.cards = [] def __str__(self): # return a string representation of A return "A contains " ... ... Now whenever i create an object test = A() and I use the print test it will get something like (do not mind the dots) A contains ... What I want to achieve is to automatically print the object reference name instead of

Python - Timeit within a class

心不动则不痛 提交于 2019-12-23 13:24:16
问题 I'm having some real trouble with timing a function from within an instance of a class. I'm not sure I'm going about it the right way (never used timeIt before) and I tried a few variations of the second argument importing things, but no luck. Here's a silly example of what I'm doing: import timeit class TimedClass(): def __init__(self): self.x = 13 self.y = 15 t = timeit.Timer("self.square(self.x, self.y)") try: t.timeit() except: t.print_exc() def square(self, _x, _y): print _x**_y