Autosave: 2023-02-16 15:15:16

This commit is contained in:
thomasabishop 2023-02-16 15:15:16 +00:00
parent 53c5012ce9
commit d69aa9cfe5
4 changed files with 62 additions and 17 deletions

View file

@ -1,17 +0,0 @@
# BBC Python Course notes
## Day 2
With lists you have to use copy if you wish to make a new version. You cannot just reassign to a new version. This will still update the original. Since it copies the pointer.
Distinguish functions that will create new list and methods which will modify existing list
Functions: named parameter passing, use for default parameter values
Python does not have constants but has a convention of upper case to mimic constants
More on addresses and pointers in Python
With classes we don't need to use `new` when instantiating an instance of a class.
You do not need to define properties in classes if they exist in the constructor

View file

@ -0,0 +1,50 @@
---
categories:
- Programming Languages
tags: [python, OOP]
---
# Classes in Python
## General points
- We don't need to use `new` when instantiating an instance of a class.
- All properties and methods must be defined in the constructor. This is in contrast to JavaScript where the properties in the constructor are those we want to initialise when the object is created and where there may be other properties and methods that are used outside of the instantiation process.
- In contrast to TypeScript and other OOP languages it is not necessary to declare the properties you wish to define in the constructor, outside of the constructor. As long as they are defined in the constructor they are accessible.
## Basic syntax
```py
class Person:
""" An example class to hold a persons name and age"""
def __init__(self, name, age):
self.name = name
self.age = age
def birthday(self):
print('Happy birthday you were', self.age)
self.age += 1
print('You are now', self.age)
p1 = Person('John', 36)
p2 = Person('Thomas', 34)
print(p1)
print(p2)
print(p1.name)
# <__main__.Person object at 0x102e75510>
# <__main__.Person object at 0x102e75511>
# John
```
Key points to note:
- The `__init__` method is the constructor function and must exist on every class to define the properties of the class
- Every object that the class instantiates is a unique intem in memory as indicated by the hexadecimal log.
- `self` is a reference to the class itself and the object it will create, akin to `this` in other languages
- You must pass `self` as a parameter to every method (this is a difference from JS)
- As with functions, we can use docstrings to document the class. What you write here will show up in Intellisense etc
## The `str` method

View file

@ -18,6 +18,14 @@ Lists have the following properties:
> Lists are denoted with `[...]`
## Duplicating lists
When we want to duplicate a list, we can't just reassign the list to a new variable and expect this to be a copy.
If we edit this "copy" it will update the original list since it copies the pointer and will therefore point to the same address in memory. Instead we have to use the List `copy()` function which returns a new list and doesn't modify the original list.
Relatedly, we should distinguish those List methods and functions which create a new list (non-destructive) and those that mutate the existing list.
## Basic usage
```python

View file

@ -15,3 +15,7 @@ To name a variable or method with multiple words we use underscores to separate
an_int = 32
print(an_integer.is_integer) # true
```
## Constants
There are no constants in Python. However there a pseudo-constants established by convention. To denote that a variable should not change you use `UPPER_CASE`.