Python : Assert that variable is instance method?

inspect.ismethod is what you want to find out if you definitely have a method, rather than just something you can call. import inspect def foo(): pass class Test(object): def method(self): pass print inspect.ismethod(foo) # False print inspect.ismethod(Test) # False print inspect.ismethod(Test.method) # True print inspect.ismethod(Test().method) # True print callable(foo) # True print callable(Test) # True … Read more

Polymorphism with instance variables [duplicate]

There is no polymorphism for fields in Java. There is however, inheritance. What you’ve effectively done is create two fields in your Rectangle class, with the same name. The names of the field are, effectively: public class Rectangle { public int Shape.x; public int Rectangle.x; } The above doesn’t represent valid Java, its just an … Read more

Ruby on Rails – Access controller variable from model

You shouldn’t generally try to access the controller from the model for high-minded issues I won’t go into. I solved a similar problem like so: class Account < ActiveRecord::Base cattr_accessor :current end class ApplicationController < ActionController::Base before_filter :set_current_account def set_current_account # set @current_account from session data here Account.current = @current_account end end Then just access … Read more

How to create a list of objects?

Storing a list of object instances is very simple class MyClass(object): def __init__(self, number): self.number = number my_objects = [] for i in range(100): my_objects.append(MyClass(i)) # Print the number attribute of each instance for obj in my_objects: print(obj.number)

“No enclosing instance of type” error while calling method from another class in Android

In terms of the actual error here, if parseYouTubeAndYahoo class is a non-static inner class inside of your Activity, then you need an instance of the enclosing class in order to instantiate the inner class. So, you’ll need: MainActivity myActivity = new MainActivity(); MainActivity.parseYouTubeAndYahoo asyncTask = myActivity.new parseYouTubeAndYahoo(); However…. You really shouldn’t be instantiating non-static … Read more

Java: newInstance of class that has no default constructor

Call Class.getConstructor() and then Constructor.newInstance() passing in the appropriate arguments. Sample code: import java.lang.reflect.*; public class Test { public Test(int x) { System.out.println(“Constuctor called! x = ” + x); } // Don’t just declare “throws Exception” in real code! public static void main(String[] args) throws Exception { Class<Test> clazz = Test.class; Constructor<Test> ctor = clazz.getConstructor(int.class); … Read more

error code: 523