python 实例方法调用时可以忽略第一个参数,未绑定的方法必须调用实例作为第一个参数 - python...

I keep on receiving the error: TypeError: unbound method get_num_students() must be called with Student instance as first argument (got nothing instead)

I need to have this project done by tonight...

Any and all help is well appreciated.

Here is the code:

class Student(object):

num_students = 0

num_grad_2013 = 0

def __init__(self, first_name, last_name, id_num, yr_of_grad, counselor):

self = self

self.first_name = first_name

self.last_name = last_name

self.id_num = int(id_num)

self.yr_of_grad = int(yr_of_grad)

self.counselor = counselor

def to_string(first_name, last_name, id_num, yr_of_grad, counselor):

print first_name

print last_name

print id_num

print yr_of_grad

print counselor

def move():

num_students -= 1

if yr_of_grad == 12:

num_grad_2013 -= 1

else:

None

print "Student with ID number: %s has moved." % (id_num)

def grad_early():

num_students -= 1

num_grad_2013 -= 1

print "Student with ID number: %s is graduating early." % (id_num)

def get_num_students():

print "There are %s students in this school." % (num_students)

def get_grad_2013():

print "There are %s students graduating this year." % (num_grad_2013)

def main():

print "Creating student Nathan Lindquist"

nathan = Student("Nathan", "Lindquist", 11111, 2014, "Iverson")

print nathan

print "Creating student Dylan Schlact"

dylan = Student("Dylan", "Schlact", 22222, 2012, "Greene")

print dylan

print "Creating student Matt Gizzo"

matt = Student("Matt", "Gizzo", 33333, 2013, "Connor")

print matt

# so number of students is 3, one is graduating in 2013

Student.get_num_students()

Student.get_grad_2013()

# change some things!

nathan.grad_early()

print nathan

matt.move()

#matt.grad_early()

#print matt

# so number of students is 2, one is graduating in 2013

Student.get_num_students()

Student.get_grad_2013()

return

Here is the Python output:

>>> main()

Creating student Nathan Lindquist

Creating student Dylan Schlact

Creating student Matt Gizzo

Traceback (most recent call last):

File "", line 1, in

main()

File "C:\Users\admin\Desktop\Python\student.py", line 51, in main

Student.get_num_students()

TypeError: unbound method get_num_students() must be called with Student instance as first argument (got nothing instead)

Also, if somebody could give me help with it printing the student as a space in memory, I would also appreciate it!

Thank you!

解决方案

It seems like you wanted to define grad_early, get_num_students and get_grad_2013 as class methods, but you declared them as instance methods instead.

An instance method is a method that, well, belongs to an instance of the class.

An example would be

class Student(object):

# ...

def print_name(self): # This is an instance method

print "executing instance method"

@classmethod

def num_of_students(cls)

print "executing class method"

The difference is that an instance method will work on

s = Student()

s.print_name()

And a class method will work on the class itself

Student.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值