Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Defining Methods | Best Practices: Python OOP
Best Practices: Python OOP

book
Defining Methods

A class can also have methods, which are functions that are associated with a class. Methods are defined just like functions, but are indented within the class definition and take the self argument as their first parameter.

Tehtävä

Swipe to start coding

  1. Create a new method called bark;
  2. Access the name attribute inside the method.

Ratkaisu

class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed

def bark(self):
print(f"{self.name} says Woof!")

Mark tasks as Completed
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 5
AVAILABLE TO ULTIMATE ONLY
some-alt