Link to home
Create AccountLog in
Avatar of drewmore44
drewmore44

asked on

Python variable declarations

I'm trying to access a variable created in one class (Roster) in another class (main), but I keep getting the following error when I run the main class:

Traceback (most recent call last):
  File "C:\Users\drew\Documents\programs\CS\main.py", line 44, in <module>
    Roster.getServers()
AttributeError: 'module' object has no attribute 'getServers'


Here are my two classes. I have also tried defining the Roster class in the same file as the main class, but get the same error (only 'module' is replaced with 'Roster'). I have a feeling this problem springs from a basic misunderstanding on my part regarding where to declare these variables. I'm a new programming student, so please help me figure out what this misunderstanding is! I'm also very open to suggestions as to a better way to go about this in general.. (I'm trying to create a static roster of employees with data that can be accessed dynamically by other classes/objects)

============Roster class===============
import main

class Roster:
    def __init__(self):
        self.DrewMoore = Emp
        self.JohnJasso = Emp
        self.MitchNeuberg = Emp
        self.MikeZwiller = Emp
        self.SalRamos = Emp
        self.Servers = [self.DrewMoore, self.JohnJasso, self.MitchNeuberg]
        self.SAs = [MikeZwiller, SalRamos]

         def getServers():
             print[self.Servers]



==================Main Class==================
import Roster
class Emp:
    def __init__(self):
        self.name = input("Enter name: ")
        self.avail = {"monL": 3, "monD": 3, "tueL": 3}

    def __str__(self):
        return(str(self.name))
               
    def Print(self):
        print(self.name)
       
Roster.DrewMoore

===================================

ps. I have also tried calling Roster.getServers(), and have tried with/without using self. in front of the variables and get the same message. Thanks in advance for any help!
Avatar of gelonida
gelonida
Flag of France image

Roster is the class.
However you did not instantiate an object of the type roster.

try for example

my_roster = Roster()
print my_roster.DrewMoore

Open in new window


there's one more thing, where you should pay attention.

As a general rule you should avoid all circular imports:

In your example it seems, that

the file declaring Emp tries to import Roster

and the file declaring Roster is using emp.
I'll send you one possible fix in a minute
SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Why did you put this question in the Java TA??
Why did you put this question in the Java TA??

Perhaps out of habit. or perhaps in order to have help from users who have a Java background and who are learning Python. Sometimes it can be helpful to ask people who know the 'old' and the 'new' language
No - it just doesn't help. Experts here who are in both camps won't need you to re-organise the topic area paradigms. They'll be able to understand the relationships themselves.
Avatar of pepr
pepr

Opinions may differ. Drewmore44 is both beginner in ExpertExchange, and in Python. I agree with gelonida that it may be good to know what the asker knows.

Try the following example. It does what you probably mean, but you should be warned that this is probably not the way that should be used in Python:
#!python2

class Emp:
    def __init__(self, name):
        self.name = name
        self.avail = {"monL": 3, "monD": 3, "tueL": 3}

    def __str__(self):
        return self.name
        
    def __repr__(self):
        return self.__class__.__name__ + '(' + repr(self.name) + ')'    


class Roster:
    DrewMoore = Emp('Moore Drew')
    MikeZwiller = Emp('Zwiller Mike')
    JohnJasso = Emp('Jasso John')
    MitchNeuberg = Emp('Neuberg Mitch')
    SalRamos = Emp('Ramos Sal')
    Servers = [DrewMoore, JohnJasso, MitchNeuberg]
    SAs = [MikeZwiller, SalRamos]

    @classmethod
    def printServersClassMethod(cls):
        print cls.Servers

    @staticmethod
    def printServersStaticMethod():
        print Roster.Servers


print Roster.DrewMoore
print Roster.Servers
Roster.printServersClassMethod()
Roster.printServersStaticMethod()

Open in new window

It prints on my console:
c:\tmp\_Python\drewmore44\Q_27933729>py a.py
Moore Drew
[Emp('Moore Drew'), Emp('Jasso John'), Emp('Neuberg Mitch')]
[Emp('Moore Drew'), Emp('Jasso John'), Emp('Neuberg Mitch')]
[Emp('Moore Drew'), Emp('Jasso John'), Emp('Neuberg Mitch')]

Open in new window

...and now I am going to fill in the comments. Stay tuned :)
Avatar of drewmore44

ASKER

okay, wow, thanks for all the responses!

first, @krakatoa: i didn't mean to post to the Java TA. I only meant to put it in Python. I might have clicked Java out of habit (as i post frequently to it), but I don't think so, I think e-e did that automatically because I usually am posting to it.

@gelolinda: thanks for the tip re: circular imports. I actually came to the same conclusion  when I was working on this thing last night (and thus moved the Emp declaration into the Roster class file), but that conclusion was purely based on intuition, the term "circular imports" is a useful one to know and helps me wrap my head around that issue. Also, thanks for the pointers re: input() and inheriting from object - I was not aware of either of these facts and I'm sure they were bound to lead to later posts on this site, haha.

@pepr: This example was very helpful. Why is this "probably not the way that should be used in python?" Also, I'm a little unclear as to what the cls parameter to the class method does.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
This was extremely helpful, thank you so much. I have some questions, but I'm going to let this simmer on the "back burner" so to speak, then re-read it again in a few hours and ask them.
Thanks for the point, but gelonida also helped you. You should split the points next time ;) Or you can ask moderator to revert the accept and then do the split. (My every-month goal is only to get 4000 points to be able to look everywhere in EE and ask everything without limits.)
I didn't realize I could do that! Yes s/he certainly did, I'm forwarding that request now
@drewmore44: Thanks. That's the way :) Move your mouse cursor over the little gray symbol here ^. You will see how much of hair the author (he or she thinks) has and in what shape. Then you can guess that gelonida is probably HE, and _alias99 is probably SHE ;)
@pepr / @drewmore44  Thanks for splitting the points
happy coding