1 of 9

CSE 163

More Objects�

Suh Young Choi�

🎶 Listening to: Shang-Chi soundtrack

💬 Before Class: Are animals better when they’re orbular or angular (aesthetically)?

2 of 9

This Time

  • Private fields and methods
  • Default parameters

Last Time

  • Objects (state and behavior)
  • References
  • Defining a class
  • Instantiating an object

2

3 of 9

Private

  • Python has no way to actually enforce this, but by convention people don’t access things that start with “_”

3

class Dog:

def __init__(self, name: str) -> None:

self._name: str = name

def bark(self) -> None:

print(self._name + ': Woof')

4 of 9

Private

  • Both fields and methods can be private if you don’t want them accessed or changed by your user
  • Private methods are often helper functions that your user doesn’t actually need

4

class SpiderFolk:

def __init__(self, name: str) -> None:

self._name: str = name

def _secret_identity(self) -> str:

if ‘spider’ not in self._name.lower():

return “Spider-Man!”

else:

return “who?”

def get_me_pictures(self, count: int) -> None:

print(f’{count} pictures of {self._secret_identity()}’)

5 of 9

“Getter” vs. “Setter” methods

  • Two broad categories of class functions that involve field manipulation
  • “Getter” methods return certain values, like fields or quick manipulations
  • “Setter” methods update fields or create new values

5

class SpiderFolk:

def __init__(self, name: str) -> None:

self._name: str = name

def get_name(self) -> str:

return self._name # a getter method

def rename(self, new_name: str) -> None:

self._name = new_name + “(renamed)” # a setter method

6 of 9

Default Parameters

  • You can use default parameters like you would before
  • You have to be careful when using objects as default values, it has some really bad unintentional side-effects

=

  • There is only one instance of the default parameter
    • In other words, they share a reference!

6

def append_to(element: int,

to: list[int] = []) -> list[int]:

to.append(element)

return to

my_list = append_to(12)

print(my_list)

my_other_list = append_to(42)

print(my_other_list)

7 of 9

Default Parameters Done Right

  • The fix is to not use an object as the default parameter, instead we usually use None

7

# Option 1

def append_to(element: int,

to: list[int] | None = None) -> list[int]:

if to is None:

to = []

to.append(element)

return to

# Option 2

def append_to(element, to=None):

to = [] if to is None else to

to.append(element)

return to

8 of 9

Lambdas

  • It’s kind of annoying to have to define a whole function just for this. It would be nice if there was a shorthand syntax

Introduce: Lambda functions– these let us define functions within the body of our code!

8

def get_dog_name(d):

return d.get_name()

sorted(dogs, key=get_dog_name)

sorted(dogs, key=lambda d: d.get_name())

9 of 9

Before Next Time

  • HW3 + LR3 due tomorrow
  • Go to section!
  • New resubmission cycle open

Next Time

  • Back in the classroom on Friday!
  • Magic methods
  • HW4 preview

9