Skip to content

Commit

Permalink
Merge pull request #235 from QuantEcon/objects-fixes
Browse files Browse the repository at this point in the history
Objects fixes
  • Loading branch information
doctor-phil authored Sep 28, 2023
2 parents 9af3667 + f5bd387 commit 9478df5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 11 additions & 8 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 @@ -679,10 +682,10 @@ class B:
return self.x + self.y
```

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.)
We can now create an object of type `MyAdder` 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
3 changes: 3 additions & 0 deletions lectures/scientific/randomness.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ kernelspec:

# Randomness

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

- {doc}`Introduction to Numpy <numpy_arrays>`
Expand Down

2 comments on commit 9478df5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.