Jump to content

[PYTHON] simple class question


rubing

Recommended Posts

I'm learing python from a WROX beginning Python book.  I am having trouble understanding their __init__(self, items={}): function.  I guess it's a constructor function that's run when the class is instantiated? Here is their code:

 

#!/usr/bin/python


class Fridge:
    """This class implements a fridge where ingredients can be
    added and removed individually, or in groups.  
    The fridge will retain a count of every ingredient added or removed,
    and will raise an error if a sufficient quantity of an ingredient 
    isn't present.
    Methods:
    has(food_name [, quantity]) - checks if the string food_name is in the fridge.  Quantity will be set to 1 if you don't specify a number.
    has_various(foods) - checks if enough of every food in the dictionary is in the fridge
    add_one(food_name) - adds a single food_name to the fridge
    add_many(food_dict) - adds a whole dictionary filled with food
    get_one(food_name) - takes out a single food_name from the fridge
    get_many(food_dict) - takes out a whole dictionary worth of food.
    get_ingredients(food) - If passed an object that has the __ingredients__ 
            method, get_many will invoke this to get the list of ingredients.
    """
    
    def __init__(self, items={}):
        """Optionally pass in an initial dictionary of items"""
        if type(items) != type({}):
            raise TypeError, "Fridge requires a dictionary but was given %s" % type(items)
        self.items = items
        return
...

 

Is 'self' a reserved word here refrencing the object?  OR just a regular  variable?

 

What is the point of this line??

 

self.items = items

 

does there really need to be a return statement?

 

??? ???

Link to comment
Share on other sites

First, self is the referencing term for the current object, just like $this in PHP. What is odd is that, in Python, you have to always define your member variables as passing the object itself as the first argument (which is done by default); hence the appearance of the "self" in the __init()__ function.

 

The self.items = items line actually assigns the passed parameter of items to an instance variable within the object itself. And, no, the return is not specifically needed.

Link to comment
Share on other sites

that is odd (kinda dumb)

 

 

so this is a constructor method?  let me see if i'm hearing you clearly obsidian. 

 

when class fridge is instantiated a dictionary list (optionally passed in), will be created and assigned to items

Link to comment
Share on other sites

when class fridge is instantiated a dictionary list (optionally passed in), will be created and assigned to items

 

Correct, although, as with PHP, that definition is setting a default value for the optional parameter. You can provide a dictionary as a parameter to be assigned to the member variable items instead of letting an empty one be assigned, if you choose.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.