Newbie OOP question

E

Ed

I'm trying to create an OOP script but havn't quite figured out to do it.
Here's and example of what I'm trying to do.

1st file. DOG.py

class DOG:
def printName(self):
print "DOG"

2nd file: runDog.py

#!/usr/bin/python
import sys
from Commands import *
x = Commands()
a.printName()


when I run runDog.py ./runDOG.py it keeps coming up with an AttributeError:
class Commands has no attribute 'printName"

What am I doing wrong?

thanks!
Ed
When
 
K

Kevin T. Ryan

Ed said:
I'm trying to create an OOP script but havn't quite figured out to do it.
Here's and example of what I'm trying to do.

1st file. DOG.py

class DOG:
def printName(self):
print "DOG"

2nd file: runDog.py

#!/usr/bin/python
import sys
from Commands import *
x = Commands()
a.printName()


when I run runDog.py ./runDOG.py it keeps coming up with an AttributeError:
class Commands has no attribute 'printName"

What am I doing wrong?

thanks!
Ed
When
First, in your runDog.py file, you have to import your DOG.py file.
From there, you need to instantiate a "DOG" object. e.g.:
import DOG
other code...

myDog = DOG.DOG()
myDog.printName()

Hopefully that helps :)
 
C

Christopher Koppler

I'm trying to create an OOP script but havn't quite figured out to do it.
Here's and example of what I'm trying to do.

1st file. DOG.py

class DOG:
def printName(self):
print "DOG"

2nd file: runDog.py

#!/usr/bin/python
import sys
from Commands import *
x = Commands()
a.printName()


when I run runDog.py ./runDOG.py it keeps coming up with an AttributeError:
class Commands has no attribute 'printName"

What am I doing wrong?

Either you're not posting all relevant code, or you didn't actually
try out the code in the form you posted it. If the former, then `a`
seems to be defined in your Commands module, and we can't see that, if
the latter, then you probably meant x.printName(), and we still need
to know about your Commands module to help you. Or else you should've
gotten another error, since `a` hasn't been assigned to or defined as
anything.
 
L

Lawrence Oluyede

Ed said:
I'm trying to create an OOP script but havn't quite figured out to do it.
Here's and example of what I'm trying to do.

1st file. DOG.py

class DOG:
def printName(self):
print "DOG"

2nd file: runDog.py

#!/usr/bin/python
import sys
from Commands import *
x = Commands()
a.printName()

What is "Commands"?
Where do you have instantiated a?
The right way to use DOG class in DOG.py is:

#!/usr/bin/env python
from DOG import DOG
dg = DOG()
dg.printName()

Nothing more, nothing less :)

when I run runDog.py ./runDOG.py it keeps coming up with an AttributeError:
class Commands has no attribute 'printName"

What class/module is Commands?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top