On Wednesday, June 26, 2013 10:46:55 AM UTC-4, Peter Otten wrote:
> Tim wrote:
> > I am not completely understanding the type function I guess. Here is an
> > example from the interpreter:
> 
> No, you are not understanding how Python namespaces work ;) 
> To get a Vspace in the global namespace you'd have to bind that name
> Vspace = type(...)
> 
> which defeats your plan of mass creation of such names. The clean way to 
> cope with the situation is to use a dict:
> 
> classnames = ["Vspace", ...]
> classes = {name: type(name, ...) for name in classnames}
> 
> Then you can access the Vspace class with 
> classes["Vspace"]
>  
> If that is inconvenient for your usecase you can alternatively update the 
> global (module) namespace:
> globals().update((name, type(name, ...) for name in classnames)
>  
> For example:
> >>> class A(object): pass
> ... 
> >>> globals().update((n, type(n, (A,), {})) for n in ["Beta", "Gamma"])
> >>> Beta
> <class '__main__.Beta'>
> >>> issubclass(Beta, A)
> True

Thank you for that great explanation. That is exactly what I needed to know!
What a fantastic group this is.
--Tim
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to