Skip to content

Commit

Permalink
Update functions.md
Browse files Browse the repository at this point in the history
  • Loading branch information
doctor-phil authored Sep 28, 2023
1 parent 9af3667 commit 678bbe3
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lectures/python_fundamentals/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ kernelspec:

# Functions

**Co-author**
> - [Philip Solimine, *UBC*](https://www.psolimine.net)
**Prerequisites**

- {doc}`Getting Started <../introduction/getting_started>`
Expand Down Expand Up @@ -637,7 +640,7 @@ We are used to defining variables like `x = dict("a": 1, "b": 2)` and then using
For example, a simple class that stores two variables would look like this:

```{code-cell} python
class A:
class MyType:
def __init__(self, x, y):
self.x = x
self.y = y
Expand All @@ -648,13 +651,13 @@ Used both internal and external to classes, the `__init__` method is a special m
A class, defined by the `class` keyword, is a blueprint for an object. It defines the attributes and methods that an object will have. An object is an instance of a class that has been created and assigned to a variable. It is created by calling the class name as if it were a function. When you call the class name, the object is created and the `__init__` method is called by default.

```{code-cell} python
a = A(1, 2)
b = A(3, 4)
# Notice that these are different objects
a = MyType(1, 2)
b = MyType(3, 4)
# Notice that these are different objects, even though they are the same type!
a == b
```

You can see that `a` and `b` are both instances of the `A` class by using the `type` function.
You can see that `a` and `b` are both instances of the `MyType` class by using the `type` function.

```{code-cell} python
type(a)
Expand All @@ -670,7 +673,7 @@ In addition to attributes, objects can also have methods. Methods are functions


```{code-cell} python
class B:
class MyAdder:
def __init__(self, x, y):
self.x = x
self.y = y
Expand All @@ -682,7 +685,7 @@ class B:
We can now create an object of type `B` and call the `add` method, in the same way that we called methods on built-in types (like the `.upper()` method on a string.)

```{code-cell} python
b = B(1, 2)
b = MyAdder(1, 2)
print(b.add())
```

Expand Down

0 comments on commit 678bbe3

Please sign in to comment.