Python Underscore

Using the underscore as a throwaway variable

Let's say it was your job to quickly gather contact information from attendees of an event to add them to your mailing list so you can market your product. The more complex the form, the more likely people will pass on filling it out. So instead of having several input boxes, you only have two: Name and Email. Because people often have more than one family name or several middle names, storing their data in a database can be tricky. You want to create a function that takes a full name from a single field and only stores the first name and last name.

Underscores are often used as throwaway variables in python. That is a variable whose value you don't care about. See the first function below for an example of how the underscore is used to collect the "extra" names so that only the first and last names are stored.

Note that since the underscore is used twice in the first function, it is simply created and overwritten--which doesn't matter because we don't care about the value it contains.

The other functions show other ways of achieving the same thing.

Comments

Post a Comment

Popular posts from this blog

Getting Started With AWS and Useful Learning Resources