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
 
Ad

Advertisements

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.
 
Ad

Advertisements

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?
 

Top