Class Flashcards
What is a class?
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.
Instantiation?
For a class to be useful we must create an instance of it.
myinstance = MyClass()
A class instance is also called an object.
Class variable?
A class variable or attribute allows every instance of the class to have access to the same varible.
Methods?
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’.
Constructor?
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() _
What is a instance variable?
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.
Attribute function?
The attribute function can be used to check if an item has a certain attribute.
dir()
Passing any object to the dir() method will return all it’s attributes and methods.
dir(my_function)
__repr__(self)
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.
What is an enumeration?
An enum is a custom defined type that allows multiple constants to be assigned simultaneously.