Mike Driscoll Profile picture
Aug 13 6 tweets 2 min read
#Python also includes a special function named `__import__()`

This function is NOT used in everyday Python programming and is in fact, usually discouraged.

But we will talk about it briefly anyway!

#python_builtins_by_driscollis

🧵🐍👇
The `__import__()` function takes the following args:

🐍 name
🐍 globals=None
🐍 locals=None
🐍 fromlist=()
🐍 level=0
You can use `__import__()` to import modules. Here's an example of import Python's `sys` module using it:
In most cases, you should use #Python's `importlib` module instead of `__import__()`. 🐍🔥

You can read more about that module here:

docs.python.org/3/library/impo…
You can learn more about #Python's `__import__()` function in the docs:

docs.python.org/3/library/func…
Thanks for checking out my brief into to Python's `__import__()` function!

• • •

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 11
You hear a lot about typing when it comes to computer programming. I'm not talking about using your keyboard ⌨️

I'm talking about:

💪 Strong typing
🚀 Dynamic typing
🚨 Static typing

What kind of typing does #Python 🐍 have? Let's talk about that!
Let's start with strong 💪 typing!

Strong typing means that variables have a type (int, str, dict, etc) and that that type matters when you perform an operation on the variable.

An example of an operation is addition or subtraction
Because of strong typing, variables need to be compatible when doing an operation on them. In #Python, you can add integers and floats together because they are compatible types.

But you can't add integers and strings together! 🐍🤯 Image
Read 11 tweets
Aug 11
Let's spend a little time learning how to say "Hello World" in different languages

And when I say different languages, I mean different programming languages!

🧵👇
My favorite programming language is #Python. 🐍

You can do a "Hello World" program in Python in one line: Image
When I first began learning about programming computers, C++ was one of the first languages that I learned in school.

"Hello World" is a bit more verbose in C++ land: Image
Read 12 tweets
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:
#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!
Read 15 tweets
Aug 3
#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
Read 4 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

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!

:(