They just need some pointers on how the new language

This preview shows page 3 - 5 out of 5 pages.

They just need some pointers on how the new language can help them achieve these goals. Advanced Topics Types and Objects object is the built-in object that serves as the ancestor of all new-style classes. type is an object of type 'type' that serves as the ancestor of all built-in types and metaclasses. If you find triple entendre in a programming language more annoying that amusing, just remember this diagram: object --> type --> metaClassX | | --> inheritance right --> \ - --> - --> ClassX | | instantiation down objX Exercise Write the simplest possible program with a metaclass, a class, and an instance of that class. Use the functions type(), issubclass(), isinstance(), to explore the relationships among these objects. >>> object <type 'object'> >>> type <type 'type'> >>> issubclass(type,object) True >>> class metaClassX(type): pass >>> class ClassX(object): __metaclass__ = metaClassX >>> objX = ClassX() >>> isinstance(objX,ClassX) True >>> isinstance(objX,metaClassX) False >>> isinstance(ClassX,metaClassX) True >>> isinstance(ClassX,object) True >>> issubclass(ClassX,object) True >>> issubclass(metaClassX,type) True
Image of page 3

Subscribe to view the full document.

>>> issubclass(metaClassX,object) True >>> issubclass(ClassX,type) False >>> type(ClassX) <class '__main__.metaClassX'> >>> type(metaClassX) <type 'type'> >>> dir(object) ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__'] >>> dir(type) ['__base__', '__bases__', '__basicsize__', '__call__', '__class__', '__cmp__', '__delattr__', '__dict__', '__dictoffset__', '__doc__', '__flags__', '__getattribute__', '__hash__', '__init__', '__itemsize__', '__module__', '__mro__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__subclasses__', '__weakrefoffset__', 'mro'] >>> ClassX <class '__main__.ClassX'> >>> dir(ClassX) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__'] >>> dir(objX) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__'] >>> ClassX.__dict__ <dictproxy object at 0x008FD590> >>> ClassX.__dict__.keys() ['__dict__', '__weakref__', '__module__', '__metaclass__', '__doc__'] >>> object.__dict__.keys() ['__setattr__', '__reduce_ex__', '__new__', '__reduce__', '__str__', '__getattribute__', '__class__', '__delattr__', '__repr__', '__hash__', '__doc__', '__init__'] >>> ### metaMadness.py class metametaClassX(type): mm1 = 'mm1' def __init__(cls,name,bases,cdict): cls.mm1 = metametaClassX.mm1 class metaClassX(type): __metaclass__ = metametaClassX m1 = 'm1' def __init__(cls,name,bases,cdict): cls.m1 = metaClassX.m1 cls.mm1 = metaClassX.mm1 class ClassX(object):
Image of page 4
__metaclass__ = metaClassX c1 = 'c1' objX = ClassX() for x in objX,ClassX,metaClassX,metametaClassX: print type(x) ###
Image of page 5

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern