-2

I'm new to Python, and I've just reached the OOP chapter in my programming textbook.

The textbook does not explain how property() works.

How does property() know to change the name to new_name and not new_name + "lololol"?

I created set_name2 because I wanted to see how property() works. But to my surprise the name did not change to new_name + "lololol".

# Property Critter

class Critter(object):
    """A virtual pet"""
    def __init__(self, name):
        print "A new critter has been born!"
        self.__name = name

    def get_name(self):
        return self.__name

    def set_name(self, new_name):
        if new_name == "":
            print "A critter's name can't be an empty string."
        else:
            self.__name = new_name
            print "Name change successful."

    def set_name2(self, new_name):
        self.__name = new_name + "lololol"
        print "Name change successful (set_name2!)"

    name = property(get_name, set_name, set_name2)

    def talk(self):
        print "\nHi, I'm", self.name


# main

crit = Critter("Poochie")
crit.talk()
print "\nMy critter's name is:",
print crit.name
print "\nAttempting to change my critter's name."
crit.name = ""
print "\nAttempting to change my critter's name again."
crit.name = "Randolph"

crit.talk()

raw_input("\n\nPress enter to exit.")
BBedit
  • 7,037
  • 7
  • 37
  • 50
  • 2
    Read this then: http://docs.python.org/2/howto/descriptor.html – Ashwini Chaudhary Mar 06 '14 at 17:14
  • @AshwiniChaudhary I have just read that webpage but I cannot find a simple explanation there. – BBedit Mar 06 '14 at 17:18
  • If you know the basics of Python classes then that's the best explanation IMO. And please ask a specific question, you're kind of asking for a tutorial on properties. – Ashwini Chaudhary Mar 06 '14 at 17:25
  • possible duplicate of [Python, how does the @property decorator work?](http://stackoverflow.com/questions/17330160/python-how-does-the-property-decorator-work) – Mp0int Mar 21 '14 at 12:00

1 Answers1

2

The third argument for property is expected to be a function that deletes the property, not another setter function (the name of the function has no special meaning, just naming it set_something doesn't make it a setter). The official documentation of property has good explanation of what fget, fset and fdel are expected to do.

To understand how property works internally, you can read about descriptors as pointed by Ashwini Chaudhary.

Here is a little modification of your code to illustrate the property behavior:

# Property Critter

class Critter(object):
    """A virtual pet"""
    def __init__(self, name):
        print "A new critter has been born!" 
        self.__name = name

    def get_name(self):
        return self.__name

    def set_name(self, new_name):
        if new_name == "":
            print "A critter's name can't be an empty string." 
        else:
            self.__name = new_name
            print "Name change successful." 

    def del_name(self):
        self.__name = "Scratchy"
        print "If you don't give me a name, I will choose one myself!" 

    name = property(get_name, set_name, del_name)

    def talk(self):
        print "\nHi, I'm", self.name 


# main

crit = Critter("Poochie")
crit.talk()
print "\nMy critter's name is:",
print crit.name 
crit.name = ""
crit.name = "Itchy"
crit.talk()
del crit.name
crit.talk()

Which outputs:

A new critter has been born!

Hi, I'm Poochie

My critter's name is: Poochie
A critter's name can't be an empty string.
Name change successful.

Hi, I'm Itchy
If you don't give me a name, I will choose one myself!

Hi, I'm Scratchy
Cilyan
  • 7,883
  • 1
  • 29
  • 37