rubing Posted October 10, 2008 Share Posted October 10, 2008 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? ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/127893-python-simple-class-question/ Share on other sites More sharing options...
obsidian Posted October 10, 2008 Share Posted October 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127893-python-simple-class-question/#findComment-662146 Share on other sites More sharing options...
rubing Posted October 10, 2008 Author Share Posted October 10, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127893-python-simple-class-question/#findComment-662305 Share on other sites More sharing options...
obsidian Posted October 13, 2008 Share Posted October 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127893-python-simple-class-question/#findComment-664034 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.