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"
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 `self.name` later on when you call the `walk()` method:
There is more information about "self" in the #Python documentation here:

docs.python.org/3/tutorial/cla…
@treyhunner also covers "self" in one of his @PythonMorsels articles:

pythonmorsels.com/topics/what-is…

β€’ β€’ β€’

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

7 Oct
I use a lot of #Python resource. Here are some of my favorite!

Yes, it's another thread!

πŸ§΅πŸ‘‡πŸ
Mouse vs Python, which is my own blog, is a resource I use myself because I write it for my future self as well as for you.

I put what I am currently working on or neat code snippets on there that I think I will use

blog.pythonlibrary.org
If I need to know about a specific module in #Python, I still check the Python Module of the Week website by @doughellmann

pymotw.com/3/
Read 11 tweets
7 Oct
Concurrency is a hard topic. So today we're going to talk about multiprocessing in #Python

Join for me another fun mini-thread!

πŸ§΅πŸ‘‡πŸ
The Pros of using `multiprocessing` in #Python (part 1 of 2)

🐍 Processes use separate memory space
🐍 Code can be more straightforward compared to threads
🐍 Uses multiple CPUs / cores
🐍 Avoids the Global Interpreter Lock (GIL)
The Pros of using `multiprocessing` in #Python (part 2 of 2)

🐍 Child processes can be killed (unlike threads)
🐍 The multiprocessing module has an interface similar to `threading.Thread`
🐍 Good for CPU-bound processing (encryption, binary search, matrix multiplication)
Read 9 tweets
7 Oct
Do you know how to make a #Python class method act like an attribute?

You can do that by turning the method into a `property`

Learn how in this thread!

πŸ§΅πŸ‘‡πŸ
The first step is to look at a regular #Python class.

The following `Person` class has a method called `full_name()`. To get the full name of the `Person`, you must call `full_name()`

Wouldn't it be nice if you could just use dot notation instead of calling it? Image
In #Python, you can turn the method into a `property` using the `@property` decorator

Once you do that, you can then use dot notation to access the `full_name()` method as if it were an instance attribute! Image
Read 10 tweets
6 Oct
There are a lot of "magic methods" or "dunder methods" in #Python.

I'll be talking about these for a few days, so stay tuned!

First off is `__init__()` and `__repr__()`
A magic method is a #Python class method that begins and ends with double-underscores

Most classes require you to create an `__init__()`

`__init__()` is short for initialization and initializes the instance Image
Sometimes, `__init__()` is called a constructor. However, I have seen it claimed that you need to combine `__new__()` with `__init__()` for it to be a true constructor

`__new__()` is used for allocating memory while `__init__()` is used for initializing the instance
Read 7 tweets
6 Oct
Whenever you talk about classes in #Python, you will probably see the `super()` function mentioned.
The most common way of writing `super()` in a #Python class is to write it like this:
You can also call `super()` with the class name and self passed in, but most #Python developers see that as redundant since it is the equivalent of `super().__init__()`
Read 5 tweets
6 Oct
Today I'm going to start a series on #Python classes.

According to the Python docs: "Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. "

πŸ§΅πŸ‘‡πŸ
Here is an example of a #Python class that has a single attribute: `name`

🐍 `animal_1` is an instance of the `Animal()` class
🐍 `animal_2` is another instance of the `Animal()` class

You can access an instance's attributes using the "." operator
A #Python class always inherits from the `object` base class. In Python 3, you do not need to specify that you inheriting from `object`

That means that the following two examples are doing the same thing:
Read 4 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

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

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(