Discover and read the best of Twitter Threads about #TerminologyTuesday

Most recents (10)

Python class curious? ๐Ÿค”

Here's a 4-in-1 thread about class-related Python terms ๐Ÿ

โ€ข class
โ€ข class instance
โ€ข method
โ€ข initializer method

Make sure to click through to each of the 4 thread for more ๐Ÿงต

#Python #TerminologyTuesday
A class is essentially a template for creating an object in Python.

Every object has a class, seen by the "type" function:

>>> type(4)
<class 'int'>

The terms "type" & "class" are synonyms in Python.

Some "functions" are actually implemented as classes.

On ๐—ฐ๐—น๐—ฎ๐˜€๐˜€๐—ฒ๐˜€ ๐Ÿ–ฑ๏ธ
Calling a class returns an "instance" of that class.

All objects are instances of their "type".

>>> x = True
>>> type(x) == bool
True
>>> isinstance(x, bool)
True

But objects are instances of parent classes as well:

>>> isinstance(x, int)
True

On ๐—ถ๐—ป๐˜€๐˜๐—ฎ๐—ป๐—ฐ๐—ฒ๐˜€ ๐Ÿ–ฑ๏ธ
Read 7 tweets
What's "assignment" in #Python?

This might seem like a simple concept, but there are some gotchas in the way assignment works in Python.

Fundamentally "assignment" describes the action of binding a name to a value.
You can assign a variable (a.k.a. name) to a value (a.k.a. object) with an "assignment statement".

That uses an = sign, like this:

>>> x = []

That assigns the name x to an empty list.

If "x" already exists, an = sign will re-assign it to a new value:

.>>> x = 4
>>> x
4
Python programmers often talk about names "binding" to objects or variables "pointing" to objects.

In Python, variables point to (or "refer to") values. Variables don't *contain* objects, but just say "look over there for the object". ๐Ÿ‘€

Beware of code like this:

x = []
y = x
Read 14 tweets
What's a "generator" in Python?

#TerminologyTuesday time โฐ

Let's discuss:
โ€ข The main reason generators are used
โ€ข The 2 ways to make a generator
โ€ข Lesser known generator features
โ€ข Why and when to use generators

Thread ๐Ÿงต๐Ÿ‘‡
Generators are typically used as lazy iterables.

By "lazy" I mean that they don't actually compute their values until they absolutely need to.

Essentially generators will "generate" their next value as soon as they're asked for it.
Here's a generator of every .py file in my home directory:

>>> from pathlib import Path
>>> py_files = Path.home().rglob("*.py")
>>> py_files
<generator object Path.rglob at 0x7f5a6721c900>

(the rglob method on pathlib.Path objects always returns a generator)
Read 21 tweets
What's a regular expression (a.k.a. regex)? ๐Ÿค” #TerminologyTuesday

Yes, they're a programming language within a programming language that's just for pattern matching and they're extremely succinct.

But what does "regular expression" really mean? And where did they come from?
Haven't seen regular expressions?

Imagine a special purpose programming language where every single character is a statement and no whitespace or comments are allowed. ๐Ÿ˜จ

Regular expressions are extremely information dense but very helpful for certain types of pattern matching.
Regular expressions are called "regular" because regular expressions define a "regular language".

What's a "regular language"?

I'm so glad you asked! This is one of the few factoids from my CS degree that actually stuck with me.

This part of CS is tightly tied to linguistics.
Read 17 tweets
What's an "object" in Python?

According to the Python glossary, an object is:

> Any data with state (attributes or value) and defined behavior (methods). Also the ultimate base class of any new-style class.

What does that really mean? (thread๐Ÿงต)

#Python #TerminologyTuesday
Usually when we think of an "object" we think of class instance. For example these are all objects:

>>> numbers = [2, 1, 3, 4, 7] # a list object
>>> colors = {"red", "green", "blue", "yellow"} # a set object
>>> name = "Trey" # a string object
>>> n = 3 # an int object
Anything that can have attributes is an object.

Anything that can has methods is an object.

Anything that you can point a variable to is an object.

Pretty much EVERY THING is an object in Python.
Read 8 tweets
class: a template for creating an object

We've defined instance and method. For this week's #TerminologyTuesday, it's finally time to define the word "class" in #Python. ๐Ÿ“•๐Ÿ

Where do classes come up and how do they work? ๐Ÿค”
The terms "class" and "type" are synonyms in Python.

The type of an object is its class and an object's class is a "type".

As we discussed when we defined "class instance" last week, every object in Python has a class. That means classes are EVERYWHERE in Python.
list, dict, int, str, tuple, bool, set, and float are all classes.

But (somewhat strangely) range, enumerate, zip, reversed, and type are also classes. ๐Ÿ˜ฆ Huh?

We often don't care whether an object is a class or a function in Python. Both are callables. ๐Ÿ‘‡
Read 11 tweets
instantiate (verb): to make an "instance" of a class

Okay... but what's an "instance"? ๐Ÿค”

instance (noun): an object

Wait, that's not a helpful definition.

Let's talk about class instances and class instantiation in Python.
#TerminologyTuesday
You can think of a "class" as a blueprint for making an objects that have specific functionality and contain particular types of data. ๐Ÿ“˜

list is a class in #Python

>>> list
<class 'list'>

An "instance of the list class" could also be called a "list object" or just "a list".
So instances of the list class (a.k.a. "lists") share their functionality with other list instances.

Here's 2 list instances:

>>> a = []
>>> b = []

They each have the same methods:

>>> a.append(2)
>>> b.append(5)

But they store separate data:

>>> a
[2]
>>> b
[5]
Read 13 tweets
Another fundamental Python term for #TerminologyTuesday

callable: an object which can be called

The classical "callable" is a function, but in #Python classes are also callables.
In many programming languages (e.g. JS, PHP, C++) creating a new "instance" of a class (an object whose type is that class) involves the "new" keyword:

let eol = new Date(2020, 1, 1);

But in #Python to make a new class instance we just call the class:

eol = date(2020, 1, 1)
The fact that classes are callables means the distinction between a function and a class is often quite subtle. All of these "functions" are actually implemented as classes:

n = float("4.5")
m = int(n)
s = str(m)
b = bool(m)
t = tuple('abcd')
e = enumerate(t)
r = reversed(t)
Read 11 tweets
Ever wondered "what's a float"?

floating point number (a.k.a. float): an object used for representing real numbers using a fairly accurate approximation

Let's talk about floats work in #Python!

#TerminologyTuesday
Floating point numbers are an approximation of their decimal equivalents.

They're super useful for most uses, but they can't be relied on for exact precision.

You can't assume that an exact equality comparison with floating point numbers will work:

>>> 0.1 + 0.02 == 0.12
False
Adding 0.1 and 0.02 results in a number that is VERY slightly different from the 0.12 number that we'd expect:

>>> 0.1 + 0.02
0.12000000000000001

This might seem like a huge problem, but it's usually not! This imprecision typically only causes trouble with strict equality.
Read 11 tweets
Here's a deceptively fuzzy #Python term: mutable object. ๐Ÿค”

mutable object: an object whose "value" can change

But what does "value" really mean?

#TerminologyTuesday
Lists are mutable because a list object can be changed.

>>> numbers = [2, 1, 3]
>>> numbers.append(4)
>>> numbers
[2, 1, 3, 4]

If two variables refer to the same list, any change to the list will be reflected by both variables:

>>> x = numbers
>>> x.clear()
>>> numbers
[]
Strings are immutable in #Python. That means that there is no way to "change" a string object in Python.

Try as hard as you might, you cannot change strings. You can only make NEW strings.

name = "Trey"
name = name + "!" # makes a new string
name += "!" # same as above
Read 12 tweets

Related hashtags

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.00/month or $30.00/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!