instance

Python check instances of classes

﹥>﹥吖頭↗ 提交于 2019-11-27 21:10:39
问题 Is there any way to check if object is an instance of a class? Not an instance of a concrete class, but an instance of any class. I can check that an object is not a class, not a module, not a traceback etc., but I am interested in a simple solution. 回答1: isinstance() is your friend here. It returns a boolean and can be used in the following ways to check types. if isinstance(obj, (int, long, float, complex)): print obj, "is a built-in number type" if isinstance(obj, MyClass): print obj, "is

Static Variable Instances and AppDomains, what is happening?

可紊 提交于 2019-11-27 20:06:31
I have public static class A { public static string ConnString; } [Serializable] public class Test{ // Accesing A's field; public string ConnString{get{return A.ConnString;}set{A.ConnString=value;}} } void Main() { A.ConnString = "InitialString"; // I set A.ConnString in the current domain var newDomain = AppDomain.CreateDomain("DomNew"); Test TObj = newDomain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, typeof(Test).FullName) as Test ; TObj.ConnString = "NewDomainString"; // It is supposed to set A.ConnString in the newDomain aka a different instance of A.ConnString // Here it is

How to remove(unregister) registered instance from Unity mapping?

主宰稳场 提交于 2019-11-27 18:31:05
I meet one problem that i can't solve now. I have the following: UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), "test", instance); where UnityHelper.DefaultContainer is my helper for getting unity container with loaded configuration. here I registered instance as an instance of IMyInterface . So anywhere( some time after using) I want to remove this mapping. Remove it at all. How I can do it? I have tried: UnityHelper.DefaultContainer.Teardown(instance) but is was unsuccessful and the following code returns instance anyway: UnityHelper.DefaultContainer.ResolveAll

No such instance field

懵懂的女人 提交于 2019-11-27 17:39:59
问题 I'm trying to get my application to save some data when the orientation of the screen is changed using the onSaveInstanceState to save a boolean value mCheated . I've set numerous break points and am getting an error for the mCheated boolean value in the variables view mCheated= No such instance field: 'mCheated' I have no idea why as I declare it with a value false when the activity is started and change it to true if a button is pressed. Can anyone help me out? package com.bignerdranch

How can I call a static method on a variable class?

佐手、 提交于 2019-11-27 15:29:40
问题 I'm trying to make some kind of function that loads and instantiates a class from a given variable. Something like this: <?php function loadClass($class) { $sClassPath = SYSPATH."/classes/{$class}.php"; if (file_exists($sClassPath)) { require_once($sClassPath); $class = $class::getInstance(); } } ?> If I use it like this: <?php loadClass('session'); ?> It should include and instantiate the session class. BTW: the static getInstance function comes from this code: <?php function getCallingClass

Comparing two instances of a class

时光毁灭记忆、已成空白 提交于 2019-11-27 15:10:51
I have a class like this public class TestData { public string Name {get;set;} public string type {get;set;} public List<string> Members = new List<string>(); public void AddMembers(string[] members) { Members.AddRange(members); } } I want to know if it is possible to directly compare to instances of this class to eachother and find out they are exactly the same? what is the mechanism? I am looking gor something like if(testData1 == testData2) //Do Something And if not, how to do so? Frederik Gheysels You should implement the IEquatable<T> interface on your class, which will allow you to

JUnit: new instance before invoking each @Test method. What are the benefits?

五迷三道 提交于 2019-11-27 15:02:45
Currently, I am reading "JUnit in action" book. In this book I found text below: JUnit creates a new instance of the test class before invoking each @Test method. This helps provide independence between test methods and avoids unintentional side effects in the test code. Because each test method runs on a new test class instance, we can’t reuse instance variable values across test methods. Now I do not see much point in this approach: For example: public class CalculatorTest { @Test public void testAdd_1() { Calculator calculator = new Calculator(); double result = calculator.add(1, 1);

Should I use “this” keyword when I want to refer to instance variables within a method?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 14:58:08
My teacher says that when I try to access an instance variable within a method I should always use the this keyword, otherwise I would perform a double search. A local scope search and then an instance scope search. Example: public class Test(){ int cont=0; public void Method(){ System.out.println(cont);//Should I use This.cont instead? } } I hope he is wrong, but I can't find any argument. No, only use this when you have a name conflict such as when a method parameter has the same name as an instance field that it is setting. It can be used at other times, but many of us feel that it simply

Writing to a static variable in an instance method, why is this a bad practice?

≯℡__Kan透↙ 提交于 2019-11-27 14:40:57
问题 I am a little confused here with this findbugs warning in eclipse. public class MyClass { public static String myString; } public class AnotherClass { public void doSomething() { MyClass.myString = "something"; } } This gives me a findbugs warning "write to static field from instance method", however this does not give me a warning: public class MyClass { public static String myString; } public class AnotherClass { public void doSomething() { doAnotherThing(); } public static doAnotherThing()

Python : Assert that variable is instance method?

℡╲_俬逩灬. 提交于 2019-11-27 14:35:32
How can one check if a variable is an instance method or not? I'm using python 2.5. Something like this: class Test: def method(self): pass assert is_instance_method(Test().method) Tom Dunham 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) #