1

Hey so I've been learning python 3.5 and I'd like some help with my classes. I wrote a class like this, but I just don't know how to make it return the value I ask it to.

I've assigned the class to a variable, but I can't get it to do what I want it.

Thanks in advance!

class Classroom(object):
    def James():
        name_james = 'James Herbert'
        age_james = '15'
        grade_james = '9'
        james_all = (age_james, grade_james, name_james)

    def print_student(self, james_all):
        self.james_all = james_all
        print (self.james_all)

x = Classroom()
x.print_student
6
  • 3
    what are you trying to achieve Commented Dec 18, 2015 at 23:20
  • @RiteshKarwa I want it to print the james_all when I call it Commented Dec 18, 2015 at 23:25
  • Can you also post the code where you instantiate the Classroom object? Commented Dec 18, 2015 at 23:27
  • there @elif i assigned it to x Commented Dec 18, 2015 at 23:32
  • You didn't call the James method. x = Classroom() x.James() x.print_student Commented Dec 18, 2015 at 23:33

3 Answers 3

3

Your problem boils down to a fundamental misunderstanding about how classes work. A class is just a template (think of a cookie cutter shape). By calling the class you are creating an instance of the class. An instance is to a class what a cookie is to a cookie cutter. The function that tells your class how to make an instance is the __init__ function. So when you call:

x = Classroom()

What happens is the __init__ function is called for Classroom. You may be asking "But I didn't ever write an __init__ function for the class, so how does that work?" Simply put, all classes in python will automatically inherit from the object class even if they don't explicitly code it in, which means they use its methods unless you explicitly override them with your own. object has a very simple __init__ that pretty much doesn't do anything.

Going back to the cookie cutter analogy, in the same way that you can make different flavors of cookie (e.g. chocolate chip, oatmeal, peanut butter) from the same cookie cutter, so too can classes make different instances when you instantiate them with different parameters. Like so (adapted from @Prune's answer):

class Student(object):
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade

    def print_student(self):
        print (self.age, self.grade, self.name)

James = Student('James Herbert', 15, 9)
James.print_student()

Diego = Student('Diego Heriberto', 14, 9)
Diego.print_student()

Which should print out:

15 9 James Herbert
14 9 Diego Heriberto

Each instance is different, and so behaves differently, but they both follow the same template laid out by the class.

If you want a Classroom class, you could do something like this:

class Classroom(object):
    def __init__(self):
        # create a list to hold students and assign the list to self
        self.students = list()

    def add_student(self, student):
        self.students.append(student)

    def print_students(self):
        # use a loop to print out all the students in the classroom instance
        for stud in self.students:
            stud.print_student()

cr = Classroom()
James = Student('James Herbert', 15, 9)
Diego = Student('Diego Heriberto', 14, 9)

cr.add_student(James)
cr.add_student(Diego)

cr.print_students()

This would have identical output to what was shown previously.

You may ask yourself "But there is the 'self' parameter that I never pass in. Is Python broken?" Python is working just fine. The self parameter is implicit when you call a method on an instance like James or Diego, but it is explicit in the actual body of the method. If you want to modify the instance in a method, then you must modify the self parameter. That is why we assign values to it in __init__. If you want to find out something about an instance, you check for it on the self parameter.

Sign up to request clarification or add additional context in comments.

Comments

2

In general, I think that question is more suited for Code Review not Stack Overflow.

Maybe you should rethink your class completely. For me it looks a lot like you wanted to model the student and not the classroom. Something like following might fit your needs better:

class Student(object):
    """A class to model students"""

    def __init__(self, age, grade, name):
        self.age = age
        self.grade = grade
        self.name = name

    def get_all_information(self):
        """Get all information about the student"""
        return (self.age, self.grade, self.name)

james = Student(15, 9, 'James Herbert')
print(james.get_all_information())

Edit: It seems others like Prune came up with the same idea.

Comments

0

The central problem is that james_all is a local variable of the method Classroom.James. It is not an attribute of the class. Unless you're within the James method, it has no useful value.

In print_student, you made it a parameter, but when you called print_student from the main program, you didn't supply the argument.

Perhaps you're looking for a general property of a student in the classroom? Have a method to initialize an object of the class and pass the defining values to that when you create a new instance.

class Student(object):
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade

    def print_student(self):
        print (self.age, self.grade, self.name)

James = Student('James Herbert', 15, 9)
James.print_student()

Output:

15, 9, 'James Herbert'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.