Classes & Objects Flashcards

1
Q

Creating a Python Class

A

Create a class using the class keyword.

class MyClass:
#define class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Creating an Object from a Class

A

You can create a new instance of an object by using the class name + ()

class Car:
pass

my_toyota = Car()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Class Methods

A

You can create a function that belongs to a class, this is known as a method.

class Car:
def drive(self):
print(“move”)
my_honda = Car()
my_honda.drive()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Class Variables

A

You can create a variable in a class. The value of the variable will be a available to all objects created from the class.

class Car:
colour = “black”
car1 = Car()
print(car1.colour) #black

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The init method

A

You will see “building car” printed.

The init method is called every time a new object is created from the class.

class Car:
def init(self):
print(“Building car”)
my_toyota = Car()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Class Properties

A

You can create a variable in the init() of a class so that all objects created from the class has access to that variable.

class Car:
def init(self, name):
self.name = “Jimmy”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Class Inheritance

A

When you create a new class, you can inherit the methods and properties of another class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly