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
https://forums.phpfreaks.com/topic/127893-python-simple-class-question/
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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.