CSC 203 Final: Class

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/22

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.

23 Terms

1
New cards

create a class

Use the keyword class followed by a capitalized class name (each word is capitalized) and a : Then indent and define attributes and methods underneath.

class Dog:

»»»def __init__(self, breed, age):

»»»»»self.breed = breed

»»»»»self.age = age

»»»def method(self):

»»»»»return or print whatever

2
New cards

def __init__(self, parameters):

Initializer. Parameters go in parentheses; first is self, then optional parameters.

3
New cards

self

The first parameter in the __init__ method is a variable that refers to an instance

4
New cards

__init__ body

Where you initialize instance attributes, typically assigning parameters to self.attribute.

def __init__(self, age)

»»»self.age = age

5
New cards

Constructor

A method that creates and initializes an object (the __init__ method).

6
New cards

Creating an instance

instance_variable = ClassName(‘argument1’, argument2)

Python passes the self argument for you so only supply the needed parameters

7
New cards

Access attributes

Use dot notation

instance.attribute

8
New cards

Methods

functions that belong to a class. They define the behavior of objects created from the class. Always include self as first parameter.

9
New cards

Call a method

Use instance.method() if self is the only parameter

10
New cards

Class attributes

Shared data for all instances of a class. It’s not in any of the functions

11
New cards

Instance methods

uses self to access the instance attribute

12
New cards

Class

A blueprint for how objects look and behave.

13
New cards

self meaning

actual instance of the class that just makes sure that all the data sticks to the correct instance

14
New cards

Dot notation

Used to access methods or attributes:

instance.attribute

instance.method()

15
New cards

Encapsulation

make data private from client code.

16
New cards

Leading underscore convention

beginning an attribute with a single or double underscore means it’s private

_attribute

__attribute

17
New cards

def __add__(self, other):

same as + operator. It takes the current and other instance. Overloads + to define how two instances of a class should be added.

ClassName( ) is used inside the method because:

  • You must return a brand new ClassName that represents the sum.

  • You should not change the original instances.

18
New cards

operator overloading

Allows custom classes to define behavior for operators like +, *, [ ].

19
New cards

Calling __add__

Use instance1 + instance2.

20
New cards

def __iadd__(self, other):

same as +=. Modifies the self object so it equals the sum of self + other

21
New cards

Calling __iadd__

Use instance1 += instance2

22
New cards

def __repr__(self):

When defined in a class, the method returns a string representation of an instance

23
New cards

Calling __repr__

Use

  • print(repr(instance))

  • print(instance)