1/16
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Characteristics of an Object
unique id created for each object
states/attributes
represented by variables
behaviour
methods performed by the object
Whats in a Class
instance variables
must start with self.
instance methods
first parameter must be self
initializer (aka. constructor)
syntax for initializer/constructor
def __init__ (self, <args>):
self.<instance_variable> = <value>
self.<instance_variable> = <value>
syntax for class
Steps for Object Instantiation
The constructor creates an object in memory
the initializer is called to initialize the object with values
Syntax for:
Object Instantiation
Calling a method
Modifying an instance variable
<object_name> = <class_name>(<args>)
<object_name> . <method_name>(<args>)
<object_name> . <instance_var> = <value>
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
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
copying objects? (no)
object1 = object2
Two names refer to the same object
object1 = object2
now object1 references object2
shallow/deep copying objects
copying objects
object1.radius = object2.radius
since object2.radius is immutable
this will actually copy object2.radius’s value
UML Diagrams
ClassName
—
InstanceVariableName : Type
—
ClassName(<parameter>:<type>) ← constructor
methodName(<parameter>:<type>) ← method
no need _innit_ method

public vs private
public
all instance variables are public
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
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
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)
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)
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