Modulo Record
Publicado por Carlos E. Ramos (4 intervenciones) el 20/08/2007 18:49:21
Para aquellos que como yo siguen el manual 'Introduccion a la programacion con Python', en el capitulo 7 existe un error ya que dicho modulo no existe; por lo tanto al llamarlo no muestra un error.
Dicho modulo aparece copiado en el apendice C del mismo manual (aunque con algunos errores de identacion):
Aqui esta el modulo:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import warnings
class metaMetaBunch(type):
def __new__(cls,classname,bases,classdict):
def __init__(self,**kw):
for k in self.__dflts__: setattr(self,k,self.__dflts__[k])
for k in kw: setattr(self,k,kw[k])
def __repr__(self):
rep=['%s=%r'%(k,getattr(self,k)) for k in self.__dflts__
if getattr(self,k)!=self.__dflts__[k]]
return '%s(%s)'%(classname,', '.join(rep))
newdict={'__slots__':[],'__dflts__':{},'__init__':__init__,'__repr__':__repr__}
for k in classdict:
if k.startswith('__'):
if k in newdict:
warnings.warn("Cant set attr %r in bunch-class %r"%(k,classname))
else:
newdict[k]=classdict[k]
else:
newdict['__slots__'].append(k)
newdict['__dflts__'][k]=classdict[k]
return type.__new__(cls,classname,bases,newdict)
class record(object):
__metaclass__=metaMetaBunch
if __name__=='__main__':
class Point(record):
x=0.0
y=0.0
color='gray'
q=Point()
print q
p=Point(x=1.2,y=3.4)
print p
r=Point(x=2.0,color='blue')
print r
print r.x,r.y
Dicho modulo aparece copiado en el apendice C del mismo manual (aunque con algunos errores de identacion):
Aqui esta el modulo:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import warnings
class metaMetaBunch(type):
def __new__(cls,classname,bases,classdict):
def __init__(self,**kw):
for k in self.__dflts__: setattr(self,k,self.__dflts__[k])
for k in kw: setattr(self,k,kw[k])
def __repr__(self):
rep=['%s=%r'%(k,getattr(self,k)) for k in self.__dflts__
if getattr(self,k)!=self.__dflts__[k]]
return '%s(%s)'%(classname,', '.join(rep))
newdict={'__slots__':[],'__dflts__':{},'__init__':__init__,'__repr__':__repr__}
for k in classdict:
if k.startswith('__'):
if k in newdict:
warnings.warn("Cant set attr %r in bunch-class %r"%(k,classname))
else:
newdict[k]=classdict[k]
else:
newdict['__slots__'].append(k)
newdict['__dflts__'][k]=classdict[k]
return type.__new__(cls,classname,bases,newdict)
class record(object):
__metaclass__=metaMetaBunch
if __name__=='__main__':
class Point(record):
x=0.0
y=0.0
color='gray'
q=Point()
print q
p=Point(x=1.2,y=3.4)
print p
r=Point(x=2.0,color='blue')
print r
print r.x,r.y
Valora esta pregunta


0