ShadyBears
ShadyBears

Reputation: 4185

Storing a string into a property

In my class, we are just starting object-oriented Python (we were doing functional programming since the beginning of the semester).

Write a new class called Sentence with the following methods: An __init__ method which takes a single parameter for a sentence as a string and stores this string in a property. Assume the sentence has no punctuation. A get_words method which returns the number of words in the sentence. Hint: split. A capitalize method which modifies the property to make sure the first letter is capitalized. Nothing is returned. Hint: upper but only to the first character.

I'm confused on the init method portion. What does it mean by "store this string in a property".

Also, irrelevant to this hmwk assignment, but why do we call the function init? Is it sort of like the "main" function like in C programming (I come from a C background).

Upvotes: 0

Views: 99

Answers (2)

abarnert
abarnert

Reputation: 365787

In Python, you can add attributes to objects at any time. For example:

class Sentence(object):
    pass

sentence = Sentence()
sentence.the_string = "This is a sentence."

An __init__ method is a special method that gets called when you construct an instance of the class. For example, that Sentence() ends up calling __init__ with no extra arguments. Because I didn't define any __init__, I get the default, which does nothing. But now let's define one, and give it an extra parameter:

class Sentence(object):
    def __init__(self, the_sentence):
        pass

sentence = Sentence("This is a sentence.")
sentence.the_string = "This is a different sentence."

Now, the only thing left is moving that attribute creation into the __init__ method:

class Sentence(object):
    def __init__(self, the_sentence):
        self.the_string = the_sentence

sentence = Sentence("This is a sentence.")

The actual question asked about storing the string in a property instead of an attribute. This is almost certainly not part of what you're actually supposed to learn, but rather a sign that your instructor or textbook or tutorial writer doesn't actually know Python very well. A property is a way of faking normal attributes on top of methods, which you shouldn't be learning about any time soon.

Upvotes: 2

Christian Ternus
Christian Ternus

Reputation: 8492

"Store this string in a property" means a property of a class, like so:

class Sentence(object):
    def __init__(self, sentence):
        self.my_sentence = sentence

In any function that's a member of this class (and that takes self as a parameter), including __init__, you can refer to my_sentence as "self.my_sentence".

Python's "special" functions are all prefixed and suffixed with __. The function is called __init__ because it's the initializer for that class, called when you instantiate a class object. For example, if you did Sentence(foo, bar), you'd be calling Sentence's __init__ method with foo and bar as arguments.

Upvotes: 1

Related Questions