0

Interesting right?
If you read this amazing tutorial by lionaneesh, you will understand why I posted this brain-boggling code.

Edited by jcao219: n/a

class TurnIntoString(type):
    def __new__(metacls, clsN, bases, clsdict):
        return "{0} from {1}.".format(clsdict.get('msg'),
                clsN)

class MyMessage(object):
    __metaclass__ = TurnIntoString
    msg = "Hello World"

print MyMessage
print MyMessage.upper()
print type(MyMessage)
print MyMessage[::-1]

#What? MyMessage, which is obviously a
#class, is now a string?
try:
    a = MyMessage() #doesn't even behave like a class anymore
except:
    print "It's no longer a class, but a string."
1
Contributor
1
Reply
3
Views
6 Years
Discussion Span
Last Post by jcao219
0

Basically, a metaclass is the class of a class.
So if you want to change a class in some way,
you can do that through metaclasses.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.