Reputation: 4755
I have a script called test.py
and inside that script I have the following:
class TestClass:
def greetings(name):
print "Hello %s!" % name
return;
def oppositeBool(value):
if value == True:
return False;
else:
return True;
In terminal I do the following to import it:
$ python
>>> import test
How do I run a method now? I'd like to do the following:
test.greetings('Superman')
and:
myNewValue = test.oppositeBool(True)
print myNewValue
Upvotes: 2
Views: 43
Reputation: 22282
Because these function are in a class, so you need call the function like this:
import test
c = test.TestClass()
c.greetings('Superman')
Sure. If you really want to call the function like test.greetings('Superman')
, don't define it in a class.
And by the way, a function in a class must with a self
variable like this:
def greetings(self, name):
print "Hello %s!" % name
return
Upvotes: 2