[Tutor] Syntax when using classes question.

Luke Paireepinart rabidpoobear at gmail.com
Sat Nov 11 01:40:25 CET 2006


Chris Hengge wrote:
> is the following dummycode good practice? Or pythonic? or what? I'm 
> just curious because there are several ways I've found to use 
> libraries and classes...
>
> Method 1:
> from lib import class
> lib().class(param1, param2)
I don't know what you're doing here but I think you may not understand 
the 'from' syntax.
It imports that into the global namespace.
so
from random import randint

will let you call
randint(0,10)

but not
random.randint(0,10)

because you never imported random.
trying to do
random.randint(0,10)
will give you a NameError because random is not defined in that namespace.

So yes, that's not pythonic, because it's not valid syntax :)
>
> Method 2: (C Style?)
> from lib import class
> myClass = class()
> myLib.class(param1, param2)
I don't understand what this 'myLib.class' comes from.
This example doesn't make sense either.

HTH,
-Luke



More information about the Tutor mailing list