• No results found

Use objects and methods

In document Real Python Part 1 (Page 32-35)

The Python programming language is an example of Object-Oriented Programming (OOP), which means that we store our information in objects. In Python, a string is an example of an object. Strings are very simple objects - they only hold one piece of information (their value) - but a single object can be very complex. Objects can even hold other objects inside of them. This helps to give structure and organization to our programming.

For instance, if we wanted to model a car, we would (hypothetically) create a Car object that holds lots of descriptive information about the car, called its attributes. It would have a color attribute, a model attribute, etc., and each of these attributes would hold one piece of descriptive information about the car. It would also include different objects like Tires, Doors, and an Engine that all have their own attributes as well.

Different objects also have different capabilities, called methods. For instance, our Car object might have a driv () method and a park() method. Since these methods belong to the car, we use them with

“dot notation” by putting them next to the object and after a period, like this:

car.park()

Methods are followed by parentheses, because sometimes methods use input. For instance, if we wanted to drive the car object a distance of 50, we would place that input of 50 in the parentheses of the “drive” method:

car.drive(50)

There are certain methods that belong to string objects as well. For instance, there is a string method calledupper() that creates an upper-case version of the string. (Likewise, there is a corresponding methodlower() that creates a lower-case version of a string.) Let’s give it a try in the interactive window:

1 >>> loudVoice = "Can you hear me yet?"

2

We created a stringloudVoice, then we called its upper() method to return the upper-case version of the string, which we print to the screen.

NOTE: Methods are just functions that belong to objects. We already saw an example of a general-purpose function, the len() function, which can be used to tell us the length of many different types of objects, including strings. This iswhy we use the length function differently, by only saying:len(loudVoice)

Meanwhile, we use dot notation to call methods that belong to an object, like when we call the upper() method that belongs to the string loudVoice:loudVoice.upper()

Let’s make things more interactive by introducing one more general function. We’re going to get some input from the user of our program by using the function raw_input(). The input that we pass to this function is the text that we want it to display as a prompt; what the function actually does is to receive additional input from the user. Try running the following script:

1 userInput =raw_input("Hey, what's up? ")

2

3 print "You said:", userInput

NOTE: Python 3 note: The raw_input() function has been renamed to just input() in Python 3. There is an input() function in Python 2 as well, but it actually works differ-ently (and its equivalent doesn’t exist in Python 3).

When you run this, instead of the program ending and taking you back to the>>> prompt, you’ll just see:

1 >>>

2

3 Hey, what's up?

…with a blinking cursor. It’s waiting for you to answer! Enter a response, and it will store that answer in the userInput string and display it back:

1 >>>

2

3 Hey, what's up? Mind your own business.

4

5 You said: Mind your own business.

6 7 >>>

Now we’ll combine the functionraw_input() with the string method upper() in a script to modify the user’s input:

1 response = raw_input("What should I shout? ")

2

3 response = response.upper()

4

5 print "Well, if you insist...", response

Callingresponse.upper() didn’t change anything about our response string. The upper() method only returned the upper-case version of the string to us, and it was up to us to do something with it.

That’s why we had to setresponse = response.upper() in order to reassign the value of the string response to its own upper-case equivalent.

In IDLE, if you want to see all the methods can apply to a particular kind of object, you can type that object out followed by a period and then hit CTRL+SPACE. For instance, first define a string object in the interactive window:

1 >>> myString = "kerfuffle"

Now type the name of your string in again, followed by a period (without hitting enter):

1 >>> myString.

When you hit CTRL+SPACE, you’ll see a list of method options that you can scroll through with the arrow keys. Strings have lots of methods!

A related shortcut in IDLE is the ability to fill in text automatically without having to type in long names by hitting TAB. For instance, if you only type in “myString.u” and then hit the TAB key, IDLE will automatically fill in “myString.upper” because there is only one method belonging to myString that begins with a “u”. In fact, this even works with variable names; try typing in just the first few letters of “myString” and, assuming you don’t have any other names already defined that share those first letters, IDLE will automatically complete the name “myString” for you when you hit the TAB key.

Review exercises:

1. Write a script that takes input from the user and displays that input back

2. Use CTRL+SPACE to view all the methods of a string object, then write a script that returns the lower-case version of a string

In document Real Python Part 1 (Page 32-35)