Class Flashcards

1
Q

What is a class?

A

A class is a template for a data type. It describes what kind of information the class will hold and how to interact with the data.
We use the ‘class’ keyword to define a class.

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

Instantiation?

A

For a class to be useful we must create an instance of it.
myinstance = MyClass()

A class instance is also called an object.

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

Class variable?

A

A class variable or attribute allows every instance of the class to have access to the same varible.

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

Methods?

A

A method is a function defined as part of a class. The first argument in a method is always the object calling the method.By convention the is usually called ‘self’.

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

Constructor?

A

There are several special methods that can be defined in classes, they are sometimes referred to as magic or dunnder methods. You can identify them by the double underscore.
A constructor is a special method used to ‘set up an object when it is instantiated.

_ init() _

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

What is a instance variable?

A

Even though a class is the blueprint for an object. When an object is created it can maintain seperate data from another object created from the same class, this in essence is the instance variable, the opposite of a class variable.

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

Attribute function?

A

The attribute function can be used to check if an item has a certain attribute.

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

dir()

A

Passing any object to the dir() method will return all it’s attributes and methods.

dir(my_function)

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

__repr__(self)

A

The __repr__() dunder method when defined in a class will tell python what the string representation of the class should be when we print out an object of that class. The method only takes self as an argument and has to return a string. If __repr__() is not used only the class name and memory address of the object will be printed.

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

What is an enumeration?

A

An enum is a custom defined type that allows multiple constants to be assigned simultaneously.

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