[Tutor] Store Class in Tuple Before Defining it ...

Kent Johnson kent37 at tds.net
Sat Aug 29 01:31:03 CEST 2009


On Fri, Aug 28, 2009 at 5:21 PM, Damon Timm<damontimm at gmail.com> wrote:
> Sorry for the double post!  Went off by mistake before I was done ...
>
> Anyhow, I would like to have a tuple defined at the beginning of my
> code that includes classes *before* they are defined ... as such (this
> is on-the-fly-hack-code just for demonstrating my question):
>
> VIDEO_TYPES = (
>    (SyncYoutube,
> re.compile(r'([^(]|^)http://www\.youtube\.com/watch\?\S*v=(?P<youtubeid>[A-Za-z0-9_-]+)\S*'),),
>    (SyncVimeo, re.compile(#more regex here#),),
>    (SyncBlip, re.compile(#more regex here#),),
> )
>
> class Video(object):
>   url = "http://youtube.com/xxxx"
>   #variables ...
>
>   def sync(self):
>       for videotype in VIDEO_TYPES:
>           #check the url against the regex,
>           # if it matches then initiate the appropriate class and
> pass it the current "self" object
>           sync = videotype[0](self).sync()
>
> class SyncYoutube(object):
>    def __init__(self,video):
>       self.video = video
>
>    def sync(self):
>         #do some custom Youtube syncing here
>
> class SyncBlip(object):
>     #etc
>
>
> This way, I can get any video object and simply run Video.sync() and
> it will figure out which "sync" to run.  However, I am finding (of
> course) that I can't reference a class that hasn't been defined!

Just move the definition of VIDEO_TYPES after the def'n of the classes
it uses. Video.sync() will compile just fine, it doesn't need
VIDEO_TYPES to be defined until it is executed.

Kent


More information about the Tutor mailing list