This follows on from a previous query of mine Static initializer in Python I am trying to find an object from one of it's properties as I would with a static constructor in java:
public class TestBuilder {
private String uid;
private String name;
private double speed;
private static ArrayList<TestBuilder> types = new ArrayList<TestBuilder>();
public static final TestBuilder SLEEPY;
public static final TestBuilder SPEEDY;
static {
SLEEPY = new TestBuilder("1", "slow test", 500.00);
SPEEDY = new TestBuilder("2", "fast test", 2000.00);
}
private TestBuilder(String uid, String name, double speed){
this.uid = uid;
this.name = name;
this.speed = speed;
types.add(this);
}
public static TestBuilder getBuilderFromName(String s){
for (int i = 0; i < types.size(); i++){
if (((TestBuilder) types.get(i)).name.equals(s)){
return (TestBuilder) types.get(i);
}
}
return null;
}
In python I have tried:
class TestBuilder:
uid = str()
name = str()
speed = float()
types = []
def __init__(self, uid, name, speed):
self.uid = uid
self.name = name
self.speed = speed
self.types.append(self)
@classmethod
def lookupType(cls, name):
for item in cls.types:
if item.name == name:
return cls
TestBuilder.SLEEPY = TestBuilder("1","slow test", 500.0)
TestBuilder.SPEEDY = TestBuilder("2","fast test", 2000.0)
Then in a test module:
class Tester(unittest.TestCase):
def test_builder(self):
dummy = TestBuilder
ref = dummy.SPEEDY
objfound = dummy.lookupType("slow test")
logger.debug("--test_builder() name: "+objfound.name)
While the correct object is found in the lookupType method the resultant classfound name property is empty when accessed in the test. How can I access the correct TestBuilder object given one of it's properties?