Online Shopping : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 2 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

Variable 'superclass'es?

Mar 31st, 2009 10:39
engatoo engatoo, Roj Merry, alex martin, Nathan Wallace, Hans Nowak, Snippet 168, Neel Krishnaswami


"""
Packages: oop
"""
"""
>I think the word is 'superclass'..hm. Anyways.
>
>What I want is a class, that inherits from a variable class.
>See what i'm trying to do? Can it be done?
I see what you are doing to do. Two thoughts come to mind:
1. Python can do it, but the way you are doing it won't work.
2. However, it's probably a bad idea.
3. But it's fun to do anyway.
Let's deal with point one first. Your method won't work because of 
the way that Python resolves names. It does things by object reference,
when you write something like 
class Foo(Bar):
    [...]
The class Foo inherits from the *object* pointed to by the name Bar, at
the time of initialization. (Remember that in Python, class and 
function
definitions are runtime statements.) This means that if you later 
write:
Bar = Quux
then the superclass of Foo won't change. In order to do what you 
want, you'll need to write a factory function to create the class
dynamically. Here's what you need:
"""
class Parent1:
    pass
class Parent2:
    pass
def ChildFactory(parent):
    class Child(parent):
        pass
    return Child
"""
Testing this in the interpreter, we 
see:
"""
Child1 = ChildFactory(Parent1)
Child2 = ChildFactory(Parent2)
print Child1.__bases__
#(<class __main__.Parent1 at 80d5ce8>,)
print Child2.__bases__
#(<class __main__.Parent2 at 80d96f0>,)
"""
The __bases__ special attribute of a class object displays the 
superclasses of that class. We can't assign to it, though, so
you can't change a class's superclasses that way. (Though you
can change an instance's class by assigning to its __class__
method.)
There are some funny side-effects to this approach that you
should remember when debugging. If you have two instances, 
each an instance of a different class produced by the factory
function:
"""
kid1 = Child1()
kid2 = Child2()
"""
If you print the class name using the __class__ attribute you
will get the name of the class definition in the ChildFactory
function:
"""
print kid1.__class__
#__main__.Child
print kid2.__class__
#__main__.Child
"""
This works ok when in actual code, though:
"""
print kid2.__class__ == kid1.__class__
# 0
"""
It's just that the visual messages can be a bit confusing.
Now, we can move on to point 2, why this is probably a bad idea.
In general, whenever you want to dynamically manipulate your 
class hierarchy, this is a sign that inheritance is the wrong
tool for the job. It's hard to read, and sometimes you will get
bitten by non-unique names inside the classes, and worst of 
all you will confuse other programmers (including yourself after
six months) because most everyone associates classes with static
things that don't change during the program run. 
Still, very often you need the dynamism? What to do -- use delegation
instead. Instead of inheriting from a class to create a new class at
runtime, you write a class that receives another object as an
initialization variable. Then each method can dispatch on the object's
class at runtime. 
"""
class Foo:
    def frob(self):
        print 'Foo!'
class Bar:
    def fiddle(self):
        print 'Bar!'
class Child:
    def __init__(self, obj):
        self.obj = obj
    def method(self):
        """A method that dispatches based on self.obj's type."""
        if self.obj.__class__ == Foo:
            self.obj.frob()
        elif self.obj.__class__ == Bar:
            self.obj.fiddle()
kid1 = Child(Foo())
kid2 = Child(Bar())
"""
Trying it out, we see:
"""
kid1.method()
'Foo!'
kid2.method()
'Bar!'
"""
Which is what we wanted. 
A third possibility (and you thought that only Perl had more than one 
way
to do it) is to create a metaclass that creates the child classes as
instances of itself. This isn't that hard either, but it would bloat 
this
post past my endurance. So someone else can post it.
All of these approaches are basically equivalent, btw. The question
is what is appropriate for your problem. And that is up to you. (Also, 
I'd
appreciate it if real Python gurus could correct any of the mistakes I
have certainly made.)
"""
http://automobile.easyfindindia.com/
http://bollywood.easyfindindia.com/
http://cloth-shopping.onlineindiadiary.com/
http://easyfind.j4ulive.com/
http://www.onlineindiadiary.com/
http://finance-guide.easyfindindia.com/
http://freelance-jobs.onlineindiadiary.com/
http://automobile.easyfindindia.com/
http://bollywood.easyfindindia.com/
http://cloth-shopping.onlineindiadiary.com/
http://easyfind.j4ulive.com/
http://www.onlineindiadiary.com/
http://finance-guide.easyfindindia.com/
http://freelance-jobs.onlineindiadiary.com/
http://gadgets.easyfindindia.com/
http://www.easyfindindia.com/
http://gift-ideas.easyfindindia.com/
http://health-fitness.easyfindindia.com/
http://home-improvement.j4ulive.com/
http://homedecor.easyfindindia.com/
http://mortgage.onlineindiadiary.com/
http://online-business.j4ulive.com/
http://online-education.j4ulive.com/
http://onlinein.j4ulive.com/
http://pets-online.easyfindindia.com/
http://realestate.easyfindindia.com/
http://sports-in-india.easyfindindia.com/
http://travel-guide.easyfindindia.com/
http://wedding-shopping.j4ulive.com/
http://www.j4ulive.com/
http://www.roo7e.com
http://www.roo7e.com
http://www.al33ab.com
http://ksacam.com
http://chat.ksacam.com
http://ksacam.info
http://oo2o.com
http://s4cam.com
http://saudi4cam.com				
http://voice.ksacam.com
http://سعودي-كام.oo2o.com
http://سعوديكام.com
http://xn--mgbply5cnrr.com
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.dir.3rabstarz.com/
http://www.up.3rabstarz.com
http://www.3rabstarz.com/vb/forumdisplay.php?f=5
http://www.3rabstarz.com/vb/forumdisplay.php?f=116
http://www.3rabstarz.com/vb/forumdisplay.php?f=66
http://www.3rabstarz.com/vb/forumdisplay.php?f=78
http://www.3rabstarz.com/vb/forumdisplay.php?f=147
http://www.3rabstarz.com/vb/forumdisplay.php?f=2
http://www.3rabstarz.com/vb/forumdisplay.php?f=31
http://www.3rabstarz.com/vb/forumdisplay.php?f=136
http://www.3rabstarz.com/vb/forumdisplay.php?f=34
http://www.3rabstarz.com/vb/forumdisplay.php?f=89
http://www.3rabstarz.com/vb/forumdisplay.php?f=26
http://www.3rabstarz.com/vb/forumdisplay.php?f=72
http://www.3rabstarz.com/vb/forumdisplay.php?f=73
http://www.3rabstarz.com/vb/forumdisplay.php?f=149
http://www.3rabstarz.com/vb/forumdisplay.php?f=150
http://www.3rabstarz.com/vb/forumdisplay.php?f=151
http://www.3rabstarz.com/vb/forumdisplay.php?f=152
http://www.3rabstarz.com/vb/forumdisplay.php?f=67
http://www.3rabstarz.com/vb/forumdisplay.php?f=3
http://www.3rabstarz.com/vb/forumdisplay.php?f=37
http://www.3rabstarz.com/vb/forumdisplay.php?f=115
http://www.3rabstarz.com/vb/forumdisplay.php?f=76
http://www.3rabstarz.com/vb/forumdisplay.php?f=77
http://www.3rabstarz.com/vb/forumdisplay.php?f=36
http://www.3rabstarz.com/vb/forumdisplay.php?f=153
http://www.3rabstarz.com/vb/forumdisplay.php?f=122
http://www.3rabstarz.com/vb/forumdisplay.php?f=118
http://www.3rabstarz.com/vb/forumdisplay.php?f=119
http://www.3rabstarz.com/vb/forumdisplay.php?f=120
http://www.3rabstarz.com/vb/forumdisplay.php?f=121
http://www.3rabstarz.com/vb/forumdisplay.php?f=35
http://www.3rabstarz.com/vb/forumdisplay.php?f=65
http://www.3rabstarz.com/vb/forumdisplay.php?f=7
http://www.3rabstarz.com/vb/forumdisplay.php?f=9
http://www.3rabstarz.com/vb/forumdisplay.php?f=11
http://www.3rabstarz.com/vb/forumdisplay.php?f=142
http://www.3rabstarz.com/vb/forumdisplay.php?f=143
http://www.3rabstarz.com/vb/forumdisplay.php?f=146
http://www.3rabstarz.com/vb/forumdisplay.php?f=140
http://www.3rabstarz.com/vb/forumdisplay.php?f=137
http://www.3rabstarz.com/vb/forumdisplay.php?f=114
http://www.3rabstarz.com/vb/forumdisplay.php?f=117
http://www.3rabstarz.com/vb/forumdisplay.php?f=113
http://www.3rabstarz.com/vb/forumdisplay.php?f=51
http://www.3rabstarz.com/vb/forumdisplay.php?f=84
http://www.3rabstarz.com/vb/forumdisplay.php?f=138
http://www.3rabstarz.com/vb/forumdisplay.php?f=139
http://www.3rabstarz.com/vb/forumdisplay.php?f=15
http://www.3rabstarz.com/vb/forumdisplay.php?f=130
http://www.3rabstarz.com/vb/forumdisplay.php?f=97
http://www.3rabstarz.com/vb/forumdisplay.php?f=102
http://www.3rabstarz.com/vb/forumdisplay.php?f=101
http://www.3rabstarz.com/vb/forumdisplay.php?f=104
http://www.3rabstarz.com/vb/forumdisplay.php?f=131
http://www.3rabstarz.com/vb/forumdisplay.php?f=103
http://www.3rabstarz.com/vb/forumdisplay.php?f=99
http://www.3rabstarz.com/vb/forumdisplay.php?f=59
http://www.3rabstarz.com/vb/forumdisplay.php?f=60
http://www.3rabstarz.com/vb/forumdisplay.php?f=39
http://www.3rabstarz.com/vb/forumdisplay.php?f=40
http://www.3rabstarz.com/vb/forumdisplay.php?f=42
http://www.3rabstarz.com/vb/forumdisplay.php?f=4
http://www.3rabstarz.com/vb/forumdisplay.php?f=1
http://www.3rabstarz.com/vb/forumdisplay.php?f=88
http://www.3rabstarz.com/vb/forumdisplay.php?f=148
http://www.3rabstarz.com/vb/forumdisplay.php?f=62
http://www.3rabstarz.com/vb/forumdisplay.php?f=63
http://www.3rabstarz.com/vb/forumdisplay.php?f=64
http://www.3rabstarz.com/vb/forumdisplay.php?f=6
http://www.3rabstarz.com/vb/forumdisplay.php?f=12
http://www.3rabstarz.com/vb/forumdisplay.php?f=96
http://www.3rabstarz.com/vb/forumdisplay.php?f=58
http://www.3rabstarz.com/vb/forumdisplay.php?f=38