That's not quite right.
When you do
module = __import__('pkg.mod')
the left-most package/module will be assigned to <module> (in this case 'pkg'),
and it will have the attribute mod. This means that members of mod aren't
directly accessible from <module>. So in order to access a member of mod, you
would still have to specify module.mod.func() (instead of the module.func()
that you might expect).
However, if you specify a non-empty fromlist, the full package will be assigned
to module, so when you do
module = __import__('pkg.mod', fromlist=['func'])
pkg.mod gets assigned to <module> and you can now call module.func()
This is a great explanation of it:
http://stackoverflow.com/questions/2724260/why-does-pythons-import-require-fromlist/2725668#2725668
On Tuesday, May 21, 2013 2:59:47 AM UTC-7, Cuchullain wrote:
> No worries - I hope I got that right.
>
>
>
> It's a super useful pattern, because if you allow users to supply
> the import-strings, you don't have to care where the module is
> deployed. It's up to the user to make sure it's accessible via the
> PYTHONPATH, or what have you.
>
>
>
> So - folks can update their code and add new behaviours without
> effort on your part.
>
>
>
> D
>
>
>
>
>
> On 21/05/13 21:48, Todd Widup wrote:
>
>
>
>
> thanks Donal, nice way of doing it :D
>
>
>
>
>
>
> On Tue, May 21, 2013 at 10:43 AM, Donal
> McMullan <[email protected]>
> wrote:
>
>
>
>
>
>
>
> if True:
>
> module_name_variable = "usermodules.todd"
>
> else:
>
> module_name_variable = "usermodules.jane"
>
>
>
> module = __import__(module_name_variable)
>
> instance = module.Klass()
>
> result = instance.prebuild()
>
>
>
> # Should have the same effect as:
>
> import usermodules.todd
>
> instance = usermodules.todd.Klass()
>
> result = instance.prebuild()
>
>
>
>
>
> I think you're correct to be wary of calling Python's
> eval:
>
> http://docs.python.org/2/library/functions.html#eval
>
>
>
>
>
>
>
>
>
>
>
> On 21/05/13 20:54, Todd Widup wrote:
>
>
>
>
>
>
>
>
>
>
>
>
> is there a python equivalent to eval?
>
>
>
> I am setting up a few things that could allow
> the user to specify a pre-build function but
> user A could specify a different prebuild
> function than user B, and I need some way of
> handling this.
>
>
>
> only thing I can think of right now is using
> maya.mel.eval("python string")
>
> which I really don't like the idea of doing
> that.
>
>
>
> any other ideas?
>
>
>
>
>
> --
>
> Todd Widup
>
> Creature TD / Technical Artist
>
> [email protected]
>
> [email protected]
>
> www.toddwidup.com
>
>
>
> --
>
> You received this message because you are subscribed
> to the Google Groups "Python Programming for
> Autodesk Maya" group.
>
> To unsubscribe from this group and stop receiving
> emails from it, send an email to
> [email protected].
>
> To post to this group, send email to
> [email protected].
>
> For more options, visit
> https://groups.google.com/groups/opt_out.
>
>
>
>
>
>
>
>
>
>
>
>
> --
>
> You received this message because you are subscribed to
> the Google Groups "Python Programming for Autodesk Maya"
> group.
>
> To unsubscribe from this group and stop receiving emails
> from it, send an email to
> [email protected].
>
> To post to this group, send email to
> [email protected].
>
> For more options, visit
> https://groups.google.com/groups/opt_out.
>
>
>
>
>
>
>
>
>
>
>
>
>
> --
>
> Todd Widup
>
> Creature TD / Technical Artist
>
> [email protected]
>
> [email protected]
>
> www.toddwidup.com
>
> --
>
> You received this message because you are subscribed to the Google
> Groups "Python Programming for Autodesk Maya" group.
>
> To unsubscribe from this group and stop receiving emails from it,
> send an email to [email protected].
>
> To post to this group, send email to
> [email protected].
>
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
>
>
>
>
>
>
> --
>
> Donal McMullan
> Production Engineer
> Desk: x6411
> Direct: +64 4380 3810
> Mobile: +64 2166 1254
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.