A question about classes.

This is the place for queries that don't fit in any of the other categories.

A question about classes.

Postby sheffieldlad » Thu Apr 11, 2013 11:41 am

Hi,

I'm trying to learn about classes.

I have defined the following class.

Code: Select all
class soldier():
    """
    none yet
    """
    def __int__(action_points, first_name, last_name, strength, morale, health, load_out, experience):
        self.action_points = action_points
        self.first_name = first_name
        self.last_name = last_name
        self.strength = strength
        self.morale = morale
        self.health = health
        self.load_out = []
        self.experience = experience
   
   


If I run this code and type the following into IDLE I get expected results as shown below.

Code: Select all
>>> tom = soldier()
>>> tom.first_name='tom'
>>> tom.last_name='smith'
>>> tom.action_points = 100
>>> tom.first_name
'tom'
>>> tom.last_name
'smith'
>>> tom.action_points
100


However, if I try to define the objects parameters as I declare a new instance of the class I get the following:

Code: Select all
>>> tony = soldier('100', 'tony', 'paradino', '100', '100', '100', 'AR50', '100')

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    tony = soldier('100', 'tony', 'paradion', '100', '100', '100', 'AR50, Knife', '100')
TypeError: this constructor takes no arguments


I presumed I could define the parameters as above but now I'm unsure. I suspect I can but I'm doing something very wrong.

Can anyone give me a gentle nudge in the right direction please?

Many thanks in advance.

-Paul
Python 2.7
Windows XP
sheffieldlad
 
Posts: 37
Joined: Sat Feb 09, 2013 3:03 pm
Location: UK

Re: A question about classes.

Postby stranac » Thu Apr 11, 2013 11:45 am

Code: Select all
    def __int__(action_points, first_name, last_name, strength, morale, health, load_out, experience):

Take a good look at that line.
There's something missing.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 897
Joined: Thu Feb 07, 2013 3:42 pm

Re: A question about classes.

Postby setrofim » Thu Apr 11, 2013 11:48 am

  • You have a typo: it should be '__init__' not '__int__'
  • Also, you're forgetting to pass self as the first argument.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: A question about classes.

Postby stranac » Thu Apr 11, 2013 11:52 am

setrofim wrote:
  • You have a typo: it should be '__init__' not '__int__'
  • Also, you're forgetting to pass self as the first argument.

Spoilers. You took all the fun out of it.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 897
Joined: Thu Feb 07, 2013 3:42 pm

Re: A question about classes.

Postby sheffieldlad » Thu Apr 11, 2013 11:54 am

stranac wrote:
def __int__(action_points, first_name, last_name, strength, morale, health, load_out, experience):

Take a good look at that line.
There's something missing.



erm......


setrofim wrote:
  • You have a typo: it should be '__init__' not '__int__'
  • Also, you're forgetting to pass self as the first argument.


Aha, you spotted it :)

I'm not sure what you mean by I forgot to pass self as the first argument.
Could you elaborate a little please?

Many thanks for the quick replies,

-Paul
Python 2.7
Windows XP
sheffieldlad
 
Posts: 37
Joined: Sat Feb 09, 2013 3:03 pm
Location: UK

Re: A question about classes.

Postby setrofim » Thu Apr 11, 2013 12:01 pm

stranac wrote:
setrofim wrote:
  • You have a typo: it should be '__init__' not '__int__'
  • Also, you're forgetting to pass self as the first argument.

Spoilers. You took all the fun out of it.

Posted before I saw your post. Sorry for spoiling your fun.

sheffieldlad wrote:I'm not sure what you mean by I forgot to pass self as the first argument.

In your code, where does the self (e.g. "self.first_name = first_name") come from? self is not a keyword (it's not like "this" in other languages). In Python the reference to the object is passed as the first argument to the method (which is conventionally called "self"). Your class declarations should be:

Code: Select all
class Soldier:
    """
    none yet
    """
    def __init__(self, action_points, first_name, last_name, strength, morale, health, load_out, experience):
        self.action_points = action_points
        self.first_name = first_name
        self.last_name = last_name
        self.strength = strength
        self.morale = morale
        self.health = health
        self.load_out = []
        self.experience = experience
   

Also, if you're on Python 2, you should use new-style classes by inheriting from object (the first line would be "class Soldier(object):").
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: A question about classes.

Postby sheffieldlad » Thu Apr 11, 2013 12:09 pm

Looks like I misunderstood how classes work in Python and I need to get my reading cap on.

Many thanks for the replies :)

-Paul.
Python 2.7
Windows XP
sheffieldlad
 
Posts: 37
Joined: Sat Feb 09, 2013 3:03 pm
Location: UK

Re: A question about classes.

Postby setrofim » Thu Apr 11, 2013 12:16 pm

See this post for links to some suggested reading.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: A question about classes.

Postby sheffieldlad » Thu Apr 11, 2013 12:19 pm

setrofim wrote:See this post for links to some suggested reading.


Many thanks, I'll take a look.

-Paul
Python 2.7
Windows XP
sheffieldlad
 
Posts: 37
Joined: Sat Feb 09, 2013 3:03 pm
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests

cron