2

I have no idea what is wrong! This is a very simple program and I have done a lot head banging! Please someone enlighten me!

This a lab problem from the CSE 111 - Programming Language II course. They teach Java at the university and the code I wrote in Java works fine.

I just have to create a Student class with some fields to hold the basic information about a student with methods to get and set the attributes. Then create an instance of that class and tryout the methods. But every time I run this program the following error occurs:

TypeError: set_name() takes exactly 1 positional argument (2 given)

Here is the code I wrote.

class Student:
    '''Student class'''

    name = None
    id = 0
    address = None
    cgpa = None

    def get_name():
        return name

    def set_name(n):
        name = n

    def get_id():
        return id

    def set_id(i):
        id = i

    def get_address():
        return address

    def set_address(a):
        address = a

    def get_cgpa():
        return cgpa

    def set_cgpa(c):
        cgpa = c


#An object of Student class
jack = Student()

jack.set_name('jacky')
print(jack.get_name())
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
ratulotron
  • 577
  • 7
  • 20

5 Answers5

6

You're not accepting a reference to your instance as the first argument to that method, i.e. your set_name() should be written:

def set_name(self, n):
    self.name = n

This is somewhat different from other languages where there is a built-in keyword (such as this) that refers to the current object. Python passes that reference explicitly, as an argument to the method.

All your other methods must be modified similarly.

Note that just setting name = n sets a local variable name which goes away when the method ends; it does not set anything on the instance. You have to explicitly set self.name if you want an instance attribute.

Also, and this is a matter of style, but you do not usually write set and get methods in Python. It is normal practice to set and get attributes directly. If you want to do validation of values, use a property instead. So basically, none of your methods are actually necessary in good style.

However, you don't have an __init__() method. Usually you would pass the desired attributes of the instance when instantiating the class and save these on the instance.

class Student:

    def __init__(self, name, id, address, cgpa):
        self.name    = name
        self.id      = id
        self.address = address
        self.cgpa    = cgpa

herman = Student("Herman Munster", 12345, "1313 Mockingbird Lane", 4.0)
kindall
  • 178,883
  • 35
  • 278
  • 309
2

Try this:

import sys 
class Student:
   '''Student class'''

   self.name = None
   self.id = 0
   self.address = None
   self.cgpa = None

   def get_name(self):
       return self.name

   def set_name(self, n):
       self.name = n

   def get_id(self):
       return self.id

   def set_id(self, i):
       self.id = i

   def get_address(self):
       return self.address

   def set_address(self, a):
       self.address = a

   def get_cgpa(self):
       return self.cgpa

   def set_cgpa(self, c):
       self.cgpa = c
barendt
  • 298
  • 3
  • 8
2

You need to pass self as the first argument to each member function of the class. Member variables must then be referred to with self, i.e. self.name. Furthermore, you may wish to include an __init__() function; this serves usually to initialize any member variables, and is called at the instantiation of the class.

Take a look at the Python documentation here for some examples on well-formed classes: http://docs.python.org/tutorial/classes.html#random-remarks

Aphex
  • 7,390
  • 5
  • 33
  • 54
1

This is because first argument of methods is self - the class instance.

See What is the purpose of self? and http://docs.python.org/tutorial/classes.html#class-objects

Community
  • 1
  • 1
warvariuc
  • 57,116
  • 41
  • 173
  • 227
  • Thanks :) I didn't know about it since I am a beginner in Python. – ratulotron Mar 02 '12 at 19:35
  • @mnzrr, yes it's unusual, but comes handy when you work more with Python. See: http://stackoverflow.com/questions/2709821/python-self-explained – warvariuc Mar 02 '12 at 19:36
  • Thanks again warwaruk! I was following several books including A Byte of Python but didn't see anything about `self` :) I have to read more carefully >:( – ratulotron Mar 02 '12 at 19:51
  • Yes :) I accepted kindall's answer since it was more descriptive :) – ratulotron Mar 02 '12 at 19:56
1

In Python, you need to pass in self for each of your member functions. You also need to reference class variables as self.x, if you want them to take an effect.

Here are a couple examples that you need to apply to the rest of your code.

   def set_name(self, n):
       self.name = n

   def get_cgpa(self):
       return self.cgpa

There is some explanation for why this is the case in the documentation.

Donald Miner
  • 38,889
  • 8
  • 95
  • 118