9 Replies - 145 Views - Last Post: 16 April 2013 - 01:57 PM Rate Topic: -----

#1 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Passing objects with parameters as parameters

Posted 16 April 2013 - 11:55 AM

I have two classes, Project and Repository. Repository holds Projects in a list. I want to add Projects to the Repository, which is very simple. However, I'd like to cut out a step, and I'm unsure of how to do that.

Here's an example Project class:

class Fruit(object):

    def __init__(self, id, linesOfCode, methods):
        self.theID = id
        self.LOC = linesOfCode
        self.theMethods = methods



Here's the Repository class:
class Repository(object):

    def __init__(self):
        self.theRepository = []

    def addProject(project):   #THIS IS MY PROBLEM!
        self.theRepository.append(project)



Now, to add a project with this format I would need to do two lines of code (after creating a repository):
rep = Repository()
myProject = Project(1, 45, 3)
rep.addProject(myProject)



However, I'd like to do this:
rep = Repository()
rep.addProject(project = Project(1, 45, 3))



Every time I do this, it keeps telling me that Project is an "undefined variable." I'm thinking this is a simple fix in the formatting of the parameter that addProject accepts, but for some reason, I can't get it to work!

Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing objects with parameters as parameters

#2 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Re: Passing objects with parameters as parameters

Posted 16 April 2013 - 12:18 PM

Fruit is supposed to be Project. I modified an old question...sorry

This post has been edited by Static Hazard: 16 April 2013 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,541
  • Joined: 12-December 12

Re: Passing objects with parameters as parameters

Posted 16 April 2013 - 12:20 PM

rep.addProject(Fruit(1, 45, 3))


or
rep.addProject(Project(1, 45, 3))

This post has been edited by andrewsw: 16 April 2013 - 12:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Re: Passing objects with parameters as parameters

Posted 16 April 2013 - 12:29 PM

View Postandrewsw, on 16 April 2013 - 01:20 PM, said:

rep.addProject(Fruit(1, 45, 3))


or
rep.addProject(Project(1, 45, 3))


I understand that, but I think that this line needs to be adjusted, right?

def addProject(project):



Also, I just tried that and I'm still getting the "undefined variable error" for Project. ????

This post has been edited by Static Hazard: 16 April 2013 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,541
  • Joined: 12-December 12

Re: Passing objects with parameters as parameters

Posted 16 April 2013 - 12:41 PM

View PostStatic Hazard, on 16 April 2013 - 07:29 PM, said:

I understand that, but I think that this line needs to be adjusted, right?

def addProject(project):



Also, I just tried that and I'm still getting the "undefined variable error" for Project. ????

Yes, I corrected that but forget to mention it:

    def addProject(self, project):   #THIS IS MY PROBLEM!
        self.theRepository.append(project)

Was This Post Helpful? 0
  • +
  • -

#6 Midwest Product  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 05-February 10

Re: Passing objects with parameters as parameters

Posted 16 April 2013 - 12:46 PM

View Postandrewsw, on 16 April 2013 - 12:41 PM, said:

View PostStatic Hazard, on 16 April 2013 - 07:29 PM, said:

I understand that, but I think that this line needs to be adjusted, right?

def addProject(project):



Also, I just tried that and I'm still getting the "undefined variable error" for Project. ????

Yes, I corrected that but forget to mention it:

    def addProject(self, project):   #THIS IS MY PROBLEM!
        self.theRepository.append(project)



Whoops, that was a typo. I actually have self included in the original code, but I left it out when I transcribed it. That code is functioning for you? I still have the exact same undefined variable for Project.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,541
  • Joined: 12-December 12

Re: Passing objects with parameters as parameters

Posted 16 April 2013 - 12:53 PM

Post your current code.

You seem to have morphed into two people?!

This post has been edited by andrewsw: 16 April 2013 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#8 Midwest Product  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 05-February 10

Re: Passing objects with parameters as parameters

Posted 16 April 2013 - 01:14 PM

Okay, here is the full version. Not sure what's going on.

This is the constructor from the Project class.
class Project(object):

    def __init__(self, id = None, locR = None, locP = None, locA = None, eP = None, eA = None):
        
         self.theID = id
         self.theLOCR = locR
         self.theLOCP = locP
         self.theLOCA = locA
         self.theEP = eP
         self.theEA = eA

         #there are some getters/setters/validation steps also, but they aren't relevant



Here is the class for Repository:

import Project5.prod.project as project

class Repository(object):

    def __init__(self):
        #constructor
        self.theContainer = []
        
    def __iter__(self):
        #makes container iterable
        return iter(self.theContainer)    
    
    def addProject(self, proj):
    
        duplicate = False
        checkId = proj.getId()
        
        #check to see if that id is already in the container if so, raise ValueError
        #append project to container
        for project in self.theContainer:
            if (project.getId() == checkId):
                duplicate = True
                break
        if(duplicate == False):
            self.theContainer.append(proj)
        else:
            raise ValueError("ProjectHistory.addProject: Duplicate ID found. Project not added to repository.")
        
        return len(self.theContainer)



I have a small driver as well. The first three lines are the working way to add a project. However, my specifications ask me to add projects like the fourth line, but I get an error that Project is an undefined variable.

hist = ProjectHistory()
myProject = project.Project(1, 10, 15, 20, 100, 200)
hist.addProject(myProject)
hist.addProject(Project(1, 10, 15, 20, 100, 200)) #error on this line, Project is an undefined variable



I would like to add a project without explicitly creating a project object, and then adding it. It's tedious, and I know there is a way to do this shortcut!

I'm not sure if I need to change this line:
def addProject(self, proj):



to:
def addProject(self, proj.Project):
#or something like this
def addProject(self, project = Project(None)):
#or what


This post has been edited by Midwest Product: 16 April 2013 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,541
  • Joined: 12-December 12

Re: Passing objects with parameters as parameters

Posted 16 April 2013 - 01:50 PM

I am unable to run the code you have posted (as it is incomplete), and I suspect that ProjectHistory might be Repository(?).

but Project is in your other file, so it appears you need project.Project() (or proj.Project()) in order to call the constructor.

BTW You might use more imaginative (meaningful) names than proj and project.

This post has been edited by andrewsw: 16 April 2013 - 01:50 PM

Was This Post Helpful? 1
  • +
  • -

#10 Midwest Product  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 05-February 10

Re: Passing objects with parameters as parameters

Posted 16 April 2013 - 01:57 PM

View Postandrewsw, on 16 April 2013 - 01:50 PM, said:

I am unable to run the code you have posted (as it is incomplete), and I suspect that ProjectHistory might be Repository(?).

but Project is in your other file, so it appears you need project.Project() (or proj.Project()) in order to call the constructor.

BTW You might use more imaginative (meaningful) names than proj and project.



got it! project.Project() made it work. I swear I had tried that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1