[an error occurred while processing this directive]
[an error occurred while processing this directive]class A: ...と書き始める「古い」スタイルのクラスと、
class A(object): ....と書き始める「新しい」スタイルのクラスがあります。
a = A() b = A() if a.__class__ == b.__class__: print "Onaji!" else : print "Chigau!"みたいに使えます。JavaだとinstanceOfってのがありますね。 さらにこわいことに、この__class__に代入することによって、動的にどのクラスのインスタンスか変えることができます。こわいこわい…
class A(object): def __init__(self): self.temperature = 20 a = A() a.__dict__["weather"] = "fine" print a.weatherいつの間にかaにweatherという変数が追加されています。こわいこわい。
class myObject(object): def load(self, fn): fd = open(fn, "r") lines = fd.readlines() cursor = 0 while cursor < len(lines): key = eval(lines[cursor]) value = eval(lines[cursor+1]) self.__dict__[key] = value cursor += 2 fd.close() def save(self, fn): fd = open(fn, "w") for key,value in self.__dict__.items(): print >> fd, repr(key) print >> fd, repr(value) fd.close()こんなふうにつかう。
class A(myObject): def __init__(self, fn = None): if fn is None: self.A = {"a":123, "b":234, "c":345} self.B = "ancde" else: self.load(fn) a = A() a.save("x") ax = A("x") print ax.A, ax.B
import struct class A(object): def __init__(self, **args): if "fromHost" in args: self.fromHost = args["fromHost"] self.toHost = args["toHost"] self.filename = args["filename"] elif "str_expr" in args: self.deserialize(args["str_expr"]) else : raise "Unsupported way to construct" def deserialize(self, msg): tip = struct.calcsize('l') cnt = 0 while len(msg) > cnt : keylen = struct.unpack('l', msg[cnt:(cnt+tip)])[0] cnt += tip key = msg[cnt:(cnt + keylen)] cnt += keylen valuelen = struct.unpack('l', msg[cnt:(cnt+tip)])[0] cnt += tip value = msg[cnt:(cnt + valuelen)] cnt += valuelen self.__dict__[key] = value def serialize(self) : ret = "" for key in ["fromHost", "toHost", "filename"]: value = self.__dict__[key] ret += struct.pack('l', len(key)) + key ret += struct.pack('l', len(value)) + value return ret def write(self) : for key in self.__dict__: value = self.__dict__[key] print " %s = %s"%(key, value) a = A(fromHost = "192.169.0.1", toHost = "192.168.0.2", filename = "hoge.c") msg = a.serialize() b = A(str_expr = msg) b.write()