-1
class courseInfo(object):

    def __init__(self, courseName):
        self.courseName = courseName
        self.psetsDone = []
        self.grade = "No Grade"

    def setGrade(self, grade):
        if self.grade == "No Grade":
         self.grade = grade

    def getGrade(self):
        return self.grade


class abc(object):
    def __init__(self, courses):
        self.myCourses = []
        self.Pset = []
        self.grade = {}
        for course in courses:
            self.myCourses.append(courseInfo(course))

     def setGrade(self, grade, course="6.01x"):
        """
        grade: integer greater than or equal to 0 and less than or 
          equal to 100
        course: string 

        This method sets the grade in the courseInfo object named 
          by `course`.   

        If `course` was not part of the initialization, then no grade is 
          set, and no error is thrown.

        The method does not return a value.
        """

    def getGrade(self, course="6.02x"):
        """
        course: string 

        This method gets the grade in the the courseInfo object 
          named by `course`.

        returns: the integer grade for `course`.  
        If `course` was not part of the initialization, returns -1.
        """

xyz = abc( ["6.00x","6.01x","6.02x"] )
xyz.setGrade(100)

print xyz.getGrade(course="6.01x")
print Xyz.getGrade(course="6.02x")

The question is how to access members of one base class from another base class in python ? Here, accessing methods of courseInfo class from abc class , without creating further subclasses?

msw
  • 42,753
  • 9
  • 87
  • 112
sonny
  • 9
  • 3
  • I don't understand your question. `abc` and `courseInfo` appear to have no relation to each other. Exactly what are you hoping to do? – Daniel Roseman May 07 '13 at 10:50
  • Umm, both your classes are inheriting from `object`. Shouldn't the second one inherit from `courseInfo`? – Burhan Khalid May 07 '13 at 10:51
  • No you see, there is one relation between the two classes, and that is in __init__ of abc class , where the list is appended by courseInfo(course). Now, normally we inherit from base class to sub class but the question is in this format and i am asking for a way to access the members of either base class without creating any further subclasses – sonny May 07 '13 at 10:55
  • possible duplicate of [Does python have 'private' variables in classes?](http://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes) – msw May 07 '13 at 11:07

4 Answers4

0

You are writing, and thinking in C++ while writing Python.

If I have an instances of courseInfo let's say c then anywhere in the program I can do any of:

c.setGrade('a')
g = c.getGrade()

or even

c.grade = 'this is not really a grade'

Every member of a class instance is 'public' and Python encapsulation works on the principle of "We're all adults here" so you don't touch my privates unless I give you permission. There is another convention that an underscore at the start of an attribute name warns you that it is a private attribute and that you really shouldn't touch it. If courseInfo has

self._grade - 'No Grade'

then you know you access _grade at your peril. This is actually true in C++ too, its just harder and involves casts.

People may answer later that self.__grade is treated differently, but it doesn't make it private and as a beginner you are best avoiding the construct until you understand why getters and setters are far less common in Python than in Java and C++.

Community
  • 1
  • 1
msw
  • 42,753
  • 9
  • 87
  • 112
0

Your question is not very clear, does this help ?

class abc(object):

    def __init__(self, courses):
        [...]
        self.my_courses = {course_name: courseInfo(course_name) 
                           for course_name in courses}

    def setGrade(self, grade, course="6.01x"):
        course = self.my_courses[course]
        course.setGrade(grade)

    def getGrade(self, course="6.02x"):
        course = self.my_courses[course]
        return course.getGrade()

Instead of a list, I store your courses in a dict to be able to look them up by name. There is other ways to do this but this is simple and elegant (and more efficient than iterating through the list every time).

Martin Maillard
  • 2,751
  • 19
  • 24
0

This appears to be simple composition, nothing to do with "accessing base classes". Your courses attribute contains all the user's courses, each of which has their own setGrade and getGrade methods. So you simply need to work out which is the relevant course inside that list, and call its methods. Something like:

for c in self.myCourses:
    if c.courseName == course:
        c.setGrade(grade)

I don't know if you have any control over the structure of abc and its __init__ method, but this would be easier if you stored the courses as a dict keyed by name, rather than a list.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

The question is in this format and I am asking for a way to access the members of either base class without creating any further subclasses

Okay, since you have a reference to all your courseInfo in myCourses, you can do this:

def setGrade(self, grade, course="6.01x"):
    for i in self.myCourses:
        if i.courseName == course:
            i.setGrade(grade)

However, you should really be doing proper inheritance.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284