Mike Driscoll Profile picture
Aug 3 4 tweets 2 min read
#Python includes a handy little function called `repr()`.

`repr()` will return a string containing a printable representation of an object.

Let's see how you might use it!

#python_builtins_by_driscollis

🧵🐍👇
The `repr()` function will call the `__repr__` dunder method of a class.

This method returns a string that is primarily for developers and will never be seen by the end-user.

Let's see what happens when you call `repr()` on a class object without a `__repr__` method Image
Now let's add a `__init__()` and a `__repr__()` method to your class so you can modify what is returned so that it is more useful

Notice that now it returns what object type it is along with the name attribute. Before, all you got back was the memory address Image
Thanks for checking out my mini-thread on the `repr()` function.

If you'd like more information about this function, check out the docs:

docs.python.org/3/library/func…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

Aug 4
Today is a great day to learn about exception handling in #Python!

Let's find out how they work!

🧵🐍👇
#Python handles exceptions using the `try/except` keywords.

You can optionally add `finally` and `else` to the mix too!

Here's an example that uses all of them: Image
#Python has the concept of the "bare except". What that means is that you catch ALL exceptions.

You can do this by catching Exception, which is the base class of ALL other exceptions.

Or you can just not specify an exception type all! Image
Read 15 tweets
Aug 2
#Python strings have TWELVE methods for testing string contents:

🐍 isalnum()
🐍 isalpha()
🐍 isascii()
🐍 isdecimal()
🐍 isdigit()
🐍 isidentifier()
🐍 islower()
🐍 isnumeric()
🐍 isprintable()
🐍 isspace()
🐍 istitle()
🐍 isupper()

🧵👇
Let's get started learning about these methods by looking at ` isalnum()`, which returns True if all characters in the string are alphanumeric and there is at least one character,
The `isalpha()` string method will return True if all characters in the string are alphabetic and there is at least one character
Read 7 tweets
Jul 26
Today we are going to talk about environment variables with #Python!

This will be a thread, so prepare yourself!

🧵🐍👇
Environment variables are variables that are available to all your programs and saved in memory by your operating system.

You can get a listing of your environment variables using #Python's `os.environ` attribute:

🐍🔥
The `os.environ` call returns a #Python dictionary.

This allows you to get environment variable values using dictionary access, like this:
Read 6 tweets
Jul 24
You need to know the basics of #Python to be able to use it effectively.

So let's talk about how you can slice a list. List slicing is where you use square brackets to "slice" out a subset of the list.

A slice is a portion of a list returned as a new list

🧵👇 Image
Here is an example of slicing a #Python list: Image
You can also use negative values when slicing a #Python list

This example demonstrates using a negative value for the beginning of the slice while ALSO not specifying the end of the slice Image
Read 5 tweets
Jul 23
Today I am going to show you how to create 7️⃣ different file types with #Python:

🐍 Text file
🐍 CSV
🐍 XML
🐍 JSON
🐍 Tar file
🐍 PDF
🐍 Excel

Join me for this fun 🧵👇
1️⃣ Creating a text file with #Python is a breeze! The recommended method is to use Python's `with` statement as it will automatically close the file for you

That means you can create a text file with TWO lines of code if you want to:
2️⃣ Creating a CSV file with #Python can be accomplished using the `csv` module

Here's a small example:
Read 16 tweets
Jul 20
When using a class in #Python, you may notice that the first argument in all the class's methods is "self"

What is up with that? Well, let's talk about it!

🧵👇🐍
First of all, "self" is a convention that refers to the instance that is created when you instantiate a class. You don't have to name it "self", but that is best practice

Here is an example where "self" is replaced with "this" Image
You use "self" to be able to access instance attributes across methods in a #Python class.

Here is an example where you initialize (__init__) `self.name` and then use `https://t.co/O7h9e3XIVz` later on when you call the `walk()` method: Image
Read 6 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(