methods

Is there a way to run a method for all instances of a class in python?

邮差的信 提交于 2019-12-06 04:54:29
问题 Example code: >>> class MyClass(object): def __init__(self, x, y): self.x = x self.y = y def power(self): print(self.x**self.y) def divide(self): print(self.x/self.y) >>> foo = MyClass(2, 3) >>> bar = MyClass(4, 7) >>> >>> foo.power() 8 >>> bar.divide() 0.5714285714285714 Whenever I used classes in Python previously, I just ran the method for each instance separately (see above). I was just wondering If there was a way to run the same method for all the instances of that class at once,

Why are Scala class methods not first-class citizens?

怎甘沉沦 提交于 2019-12-06 04:49:39
问题 I've just started Scala and am tinkering in worksheets. For example: def merp(str: String) : String = s"Merrrrrrrp $str" val merp2 = (str: String) => s"Merrrrrrrp $str" val merp3 = (str: String) => merp(str) val merp4 = merp _ merp("rjkghleghe") merp4("rjkghleghe") And the corresponding worksheet results: merp: merp[](val str: String) => String merp2: String => String = <function1> merp3: String => String = <function1> merp4: String => String = <function1> res0: String = Merrrrrrrp rjkghleghe

Fixing methods in Java OOP

蹲街弑〆低调 提交于 2019-12-06 04:48:05
I am having trouble finding the issue with my problem. Everything checks out right when I test my program except for one thing. In my print statements while I execute the program, they do not update the health correctly like they should. import java.util.Random; import java.util.Scanner; public class Dragon { private static int health; private static int attack; private static boolean isAlive = true; private static int dragonHealth; private static int dragonAttack; private static boolean isDragonAlive = true; public static int getHealth() { if(health <= 0) { health = 0; } return health; }

Accept multiple arguments in an AS3 method

試著忘記壹切 提交于 2019-12-06 04:44:31
问题 How do I accept multiple arguments in a custom method? Like: Proxy(101, 2.02, "303"); function Proxy(args:Arguments){ Task(args); } function Task(var1:int, var2:Number, var3:String){ // work with vars } 回答1: You wouldn't be able to just pass the args array through like you have in your question. You'd have to pass each element of the args array seperately. function Proxy(... args) { // Simple with no error checking. Task(args[0], args[1], args[2]); } Udate After reading one of the comments,

How to avoid ambiguity when calling Java from Matlab?

烈酒焚心 提交于 2019-12-06 04:37:44
I just discovered that when calling Java from Matlab object.method(arg1,...,argn) is equivalent to method(object, arg1,...,argn) The problem here is I also have a method.m that does some translation from Java to Matlab (eg. convert String[] to cell of strings). My method.m looks like function result = method(object, arg1,...argn) intermediate = object.method(arg1,...argn); result = translate(intermediate); What is happening is when I call method(object, arg1,...,argn) , it does the direct Java call, instead of my using my method.m The fix is easy, just don't use the same method name for both

Objective C Method Swizzling using dynamic library

有些话、适合烂在心里 提交于 2019-12-06 04:14:29
问题 I am trying to learn method swizzling. I have created a program in objective C which just calls a method within its class. Now my I am trying to load a dynamic library using DYLD_INSERT_LIBRARIES so I can override my method implementation with new method which is defined in my dynamic library. The aim is to modify the argument and then call the original function call. Program code is available at http://pastebin.com/a0b3qkgB The code for dynamic library is available at http://pastebin.com

Algorithm for finding a prime with the least amount of computations

依然范特西╮ 提交于 2019-12-06 04:07:28
问题 Assuming you were going to write a function / method to find a prime number, what would be the most efficient way to do this? I'm thinking that it would be a test that is something like this: Code Below in semi-c++ bool primeTest (int x) { //X is the number we're testing int testUpTo = (int)((sqrt(x))+1); for (int i=3; i<testUpTo; i+=2){ if ((x%i)==0) { return false; } } return true; } Does someone have a better way to go about solving this that will take less computations? edit: Changed code

Python: pass method as argument in function

被刻印的时光 ゝ 提交于 2019-12-06 03:54:02
问题 I've seen a lot of posts but none really addressing my question. With Python, I am trying to pass a method as an argument in a function which requires two arguments: # this is a method within myObject def getAccount(self): account = (self.__username, self.__password) return account # this is a function from a self-made, imported myModule def logIn(username,password): # log into account return someData # run the function from within the myObject instance myData = myModule.logIn(myObject

Generics: Make sure parameters are of same type

折月煮酒 提交于 2019-12-06 03:47:54
I've got the following method: protected <S> void setValue(final S oldValue, final S newValue) { // Do something } I want to make sure, that both parameters are of the same type. It would be cool, if there'd be a compiler error when you try to pass parameters of two different types. The above way is clearly not the correct one. I can put into a String and an Integer , since the both extend from Object . Is my want even possible? Or is the only way to make sure both parameters are of the same type to check it inside the method and throw an IllegalArgumentException ? You can do that if you

Python : Allowing methods not specifically defined to be called ala __getattr__

萝らか妹 提交于 2019-12-06 03:33:09
问题 I'm trying to write a Python class that has the ability to do the following: c = MyClass() a = c.A("a name for A") # Calls internally c.create("A", "a name for A") b = c.B("a name for B") # Calls internally c.create("B", "a name for B") A and B could be anything (well, they're defined in a database, but I don't want to explicitly define them in my code) A hacky workaround for it would be to do the following: class MyClass(): def __init__(self): self.createItem = "" def create(self, itemType,