Lecture Slide 12 - OOP

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/16

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

17 Terms

1
New cards

Characteristics of an Object

  1. unique id created for each object

  2. states/attributes
    represented by variables

  3. behaviour
    methods performed by the object

2
New cards

Whats in a Class

  • instance variables
    must start with self.

  • instance methods
    first parameter must be self

  • initializer (aka. constructor)

3
New cards

syntax for initializer/constructor

def __init__ (self, <args>):
self.<instance_variable> = <value>

self.<instance_variable> = <value>

4
New cards

syntax for class

5
New cards

Steps for Object Instantiation

  1. The constructor creates an object in memory

  2. the initializer is called to initialize the object with values

6
New cards

Syntax for:

  1. Object Instantiation

  2. Calling a method

  3. Modifying an instance variable

  1. <object_name> = <class_name>(<args>)

  2. <object_name> . <method_name>(<args>)

  3. <object_name> . <instance_var> = <value>

7
New cards

what is self.

self. represents/references the object

when a method is called, self is set to the object

self. helps us access an object’s attributes and methods

8
New cards

scope of instance variables (self.)

vs


The scope of non-instance variables (no self.) are local variables

instance variable can be accessed by all the methods in the same class

local variables (without self.) only exist in the method itself

9
New cards

copying objects? (no)

object1 = object2

Two names refer to the same object

object1 = object2

now object1 references object2

10
New cards

shallow/deep copying objects

11
New cards

copying objects

object1.radius = object2.radius

since object2.radius is immutable

this will actually copy object2.radius’s value

12
New cards

UML Diagrams

ClassName


InstanceVariableName : Type

ClassName(<parameter>:<type>) ← constructor
methodName(<parameter>:<type>) ← method

no need _innit_ method

<p>ClassName</p><p>—<br>InstanceVariableName : Type<br>—</p><p>ClassName(&lt;parameter&gt;:&lt;type&gt;) ← constructor<br>methodName(&lt;parameter&gt;:&lt;type&gt;) ← method</p><p></p><p>no need _innit_ method</p>
13
New cards

public vs private

  1. public
    all instance variables are public

  2. private
    can access within the class, but not outside
    convention: two __ before variable name
    self.__x

this is a “convention”, since python will not raise an error when we modify self.__x outside

but we can actually access and modify private instance variables outside by its new name:

object._MyClass_name

14
New cards

Name Mangling

avoids name coliiisions for instance variables/methods

changes the name starting with __ (two underscores)
to → _ClassName__variable/methodName

this is done when the object is being created

15
New cards

Accessor/Mutator Methods

To access/modify private/protected instance variables that can’t be accessed outside a class

Accessor: (retrieve/read values)

def getAge(self):

      return self.__age

Mutator: (modify values)

def setAge(self, age):
     self.__age = age

Note: public instance variables self.x can be accessed outside a class (no need accessor/mutator methods) 

16
New cards

Private instance variable

MyClass.__name = “David”

what happens when we access it outside? 

We can create a private instance variable inside class:

self.__name = “David”

Python will rename this to self._MyClass__name

accessing outside: 
usually use mutator but:

if we access outside using

print(obj.__name) #error

obj.__name = “test”

#doesn’t access original, creates a new variable __name

we need to use:

obj._Class__name = “test” 


(name mangling)

17
New cards

Class Variable

variables inside class, but not inside method

not part of an object, it’s part of the class

same value/var in all objects

to refer to it we do: <ClassName>.<VariableName> 

note:
<ObjectName>.<ClassVariableName> = <value> 
will not refer to the class variable, but rather create a new variable for that object