Re: question on creating class

2007-01-03 Thread tonisk
You can always rename your defined clas afterwards

class TestClass:
pass

myClass = TestClass

--
Tõnis

On Jan 4, 9:27 am, "wcc" <[EMAIL PROTECTED]> wrote:
> Hello,
>
> How do I create a class using a variable as the class name?
>
> For example, in the code below, I'd like replace the line
>
> class TestClass(object):
> with something like
> class eval(className) (object):
>
> Is it possible?  Thanks for your help.
>
> className = "TestClass"
>
> class TestClass(object):
> def __init__(self):
> print "Creating object of TestClass..."
>
> def method1(self):
> print "This is a method."
>
> if __name__ == "__main__":
> o = TestClass()
> o.method1()
> 
> --
> wcc

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: question on creating class

2007-01-03 Thread tonisk
Or if you have required class name in variable, then use:

class TestClass:
pass

globals()[className] = TestClass

--
Tõnis

On Jan 4, 9:27 am, "wcc" <[EMAIL PROTECTED]> wrote:
> Hello,
>
> How do I create a class using a variable as the class name?
>
> For example, in the code below, I'd like replace the line
>
> class TestClass(object):
> with something like
> class eval(className) (object):
>
> Is it possible?  Thanks for your help.
>
> className = "TestClass"
>
> class TestClass(object):
> def __init__(self):
> print "Creating object of TestClass..."
>
> def method1(self):
> print "This is a method."
>
> if __name__ == "__main__":
> o = TestClass()
> o.method1()
> 
> --
> wcc

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pow() works but sqrt() not!?

2007-01-04 Thread tonisk
you forgot to import math module

>>> import math
>>> math.sqrt(9)
3.0

if you want math functions to your current namespace use:
>>> from math import *

--
Tõnis

On Jan 4, 10:13 am, "siggi" <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> this is a newbie question on :
> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
> win32
> PC with WinXP
>
> Inhttp://www.python.org/doc/2.3.5/lib/module-math.html
> I read:
>
> "sqrt( x) Return the square root of x."
>
> Now the test for module math with function pow():
> --->>> pow(9,9)387420489
>
>  Fine, but sqrt() fails:
> --->>> sqrt(9)I get this error message
>
> 'Traceback (most recent call last):
>   File "", line 1, in 
> sqrt(9)
> NameError: name 'sqrt' is not defined'
>
> Same for sin() and cos(). ">>> Import math" does not help. Will I have to
> define the sqrt() function first? If so, how?
> 
> Please help!
> 
> Thank you,
> 
> Siggi

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pow() works but sqrt() not!?

2007-01-04 Thread tonisk
> >if you want math functions to your current namespace use:
> >>> from math import *What is a "namespace" and what is the difference 
> >>> between ">>>import math"
> and ">>>from math import *" ?

for namespaces read this
http://www.network-theory.co.uk/docs/pytut/tut_68.html

import math creates new namespace "math" for names in that module, so
you can acess them by prefixing them with "math", like math.sqrt(9).
from math import * imports all names to your local scope, so you do not
have to prefix them, sqrt(9)

-- 
Tõnis

-- 
http://mail.python.org/mailman/listinfo/python-list