Py Classes (U Bootc) Flashcards
how to see all functions and attributes belonging to an object of a class?
]] dir( )
]] help( )
1) what are functions within a class be called?
2) are there differences between built-in fn and method?
3) how are methods called?
1) Methods
2) No diff in structure and build. The diff. in structure is that methods always has a starting param. ‘self’, which is not explicitly populated when the method is called. Also, methods are not independent as a function, but are used within the context of a Class
3) Available to a class’ objects through the dot notation
name 4 different Py Classes?
integer, lists, dictionaries, tuples, strings
what can object be said to be a combination of?
Data and functions; data held by the object, to which functions available to that object can be applied
how is Class defined?
]] class Class_name( params ): ]] ''' < description > ''' ]] def \_\_init\_\_(self, params) ]] param... ]] ]] def method_name(self, params)
what is the use of ‘pass’ within a Class definition for an object?
Allows an Object to be instantiated without crashing
how to assign instance variables to an object, to be populated at the time an instance of the object is created?
init function, a method, also called a constructor
]] def init( self, var1, var2 )
]] self.var1 = var1
]] self.var2 = var2
what is a Class variable?
how is it defined?
how is it called?
what are variables called within a Class?
1) a variable value common to all instances of the type of object. Values will be the same for all object instances of the class, they can therefore be considered as default value for instances within the class 2) ]] class Class_name( ... ): ]] class_variable = var_value
3) Class variables are called with the dot.notation, as # Book class ]] class Book: ]] material = "paper"
]] Book.material # “paper”
4) Variables are called Attributes
what is a difference between a Class variable and an Instance Variable?
A Class variable’s value is common to all instances of the Class object, instance variable values is specific to each instance, and what sets one instance apart from another instance
what is a method?
a function within a class, a function to manipulate the data within the class
what is common to all params of the same class’s methods?
all have the parameter self as the 1st parameter in the list of parameters, instantiated to the class by the init-method
- self-var
- class vars
what is the naming convention for Python Classes?
CapPhrase convention; no underscores, all words are capitalised