Re: understaning "self"



On Feb 21, 6:34 am, "Poppy" <znfmail-pythonl...@xxxxxxxxx> wrote:
I've been searching online to try get a better understanding of what "self"
does when I define this parameter in my class functions. All I'm finding is
debates on whether  "self" has any value to the language but that doesn't
help me in my newbie question. So the code excerpt below is from "Beginning
Python" Norton, Samuel, Aitel, Foster-Johnson, Richardson, Diamon, Parker,
and Roberts.

What I think "self" is doing is limiting the function call to only function
in "this" class. So in the function below "has" calls self.has_various(), if
I had a function called "has_various" in my program or another included
class using "self" insures that the "has_various" below is the one used. Am
I correct in my understanding?

thanks,

Zach-

    def has(self, food_name, quantity=1):
        """
has(food_name, [quantity]) - checks if the string food_name is in the
fridge. quantity defaults to 1
returns True if there is enough, false otherwise.
"""

        return self.has_various({food_name:quantity})

    def has_various(self, foods):
        """
has various(foods) determines if the dictionary food_name
has enough of every element to satisfy a request.
returns true if there's enough, Fasle if there's not or if an element does
not exist.
"""
        try:
            for food in foods.keys():
                if self.items[food] < foods[food]:
                    return False
            return True
        except KeyError:
            return False



def bark():
print "Yip, yip."


class Dog(object):
def __init__(the_obj_that_called_this_method):
the_obj_that_called_this_method.name = "Blackie"
print "Initializing a dog object."

def bark(the_obj_that_called_this_method):
print 'My name is', the_obj_that_called_this_method.name
print "Woof, woof."


d = Dog()
d.bark()

Because the variable name: 'the_obj_that_called_this_method' is too
hard to type, by convention people use the variable name: 'self'
instead.

What I think "self" is doing is limiting the function
call to only function in "this" class.

No. Whenever you write obj.method_name, the '.' directs python to
look inside obj's class definition for the specified method. When you
call a function like this:

def show(x):
print x

show('hello')

python assigns the argument 'hello' to the parameter variable x. When
you write:

d.bark()

even though there are no arguments specified in that method call,
python secretly passes one argument to the method: the object on the
left side of the '.'.
.



Relevant Pages

  • Re: A T.O. Parable
    ... > A python comes to the edge of a river and wants to cross it. ... > nearby and asks the dog to carry him on his back across the river. ... If I let you on me you will bite me and I will die". ... A spic walks into a bar and says, "Hey, nigger, give me a drink." ...
    (alt.sports.football.pro.phila-eagles)
  • Re: Python noob SOS (any [former?] Perlheads out there?)
    ... I'm too old a dog trying to learn new tricks... ... worse, I'm so used to Perl when it comes to scripting, that I'm ... command-line options, generating of accessor functions for these ... Of course, for Python now I don't have any of this, so I must write ...
    (comp.lang.python)
  • Re: Why does python not have a mechanism for data hiding?
    ... themselves up in an argumentative position where to concede any ... position as top dog. ... All I did was to suggest that a keyword be added to Python to ... If Desthuilliers doesn't like my suggestion, ...
    (comp.lang.python)
  • Re: A T.O. Parable
    ... to change it so the python squeezes the dog to death and they both drown. ...
    (alt.sports.football.pro.phila-eagles)