Python Class Help

This is the place for queries that don't fit in any of the other categories.

Python Class Help

Postby psun03 » Sat Mar 30, 2013 7:53 pm

I am a C/C++ coder and python is slightly different to what I am use to
I am new to python (just started earlier this month)
i am trying to create a new class/module and it is giving me this error

Traceback (most recent call last):
File "deviceDemo.py", line 5, in <module>
d = Device("A","B","C")
TypeError: 'module' object is not callable

Below is my code:
This is my class code. I put this in a separate file called 'Device.py'
Code: Select all

class Device:
    dev_cnt = 0
   
    def __init__(self,alias,housecode,type):
        self.alias = alias
        self.housecode = housecode
        self.type = type
        Device.dev_cnt += 1
       
    def getStatus():
        return self.status
   
    def getAlias():
        return self.Alias
   
    def getHousecode():
        return self.housecode
   
    def getType():
        return self.type


this is what I am running

Code: Select all

#!/usr/bin/python

import Device

d = Device("A","B","C")
d.status = False;
print d.getAlias()
print d.getHousecode()
print d.getType()



Any suggestions?
psun03
 
Posts: 2
Joined: Sat Mar 30, 2013 7:47 pm

Re: Python Class Help

Postby setrofim » Sat Mar 30, 2013 8:05 pm

This is an illustaration of why you should avoid giving a module the same name as a class within that module. What you are importing is the module (.py file) Device, but you want to access the class Device inside the Device module (they are two different things).

It should be either
Code: Select all
#!/usr/bin/python

import Device

d = Device.Device("A","B","C")

or
Code: Select all
#!/usr/bin/python

from Device import Device

d = Device("A","B","C")
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Python Class Help

Postby psun03 » Sat Mar 30, 2013 8:52 pm

Thanks... again i am trying to understand python....

how about if i called the file something like library.py

should the code be something like this

Code: Select all
import library

d=Device("A","B","C")

psun03
 
Posts: 2
Joined: Sat Mar 30, 2013 7:47 pm

Re: Python Class Help

Postby stranac » Sat Mar 30, 2013 9:03 pm

When you import a module, everything inside it is available in that module's namespace.
So you'll need to prefix the things you want to access with the module's name:
Code: Select all
import library

d = library.Device("A","B","C")
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1096
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python Class Help

Postby setrofim » Sat Mar 30, 2013 9:05 pm

Nope, it should still either be
Code: Select all
import library

d = library.Device("A","B","C")

or
Code: Select all
from library import Device

d = Device("A","B","C")


In Python, each module (.py file) has its own namespace. All classes/functions/variables reside in that namespace. If you're coming from C++, you can think of it as each .py file having an implicit
Code: Select all
namespace filename-without-extension
{
// ...
}

surronding its contents. So you either have to qualify the namespace when refering to the class form that module ("libarary.Device" in Python is similar to "library::Device" in C++), or you have to use the from/import notation (which is sort of like "using namespace" clause, but only applies to the specific things you identify in the import). You can read more about how modules work in Python here.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], W3C [Linkcheck] and 2 guests