Object and Classes Flashcards
(37 cards)
To create an object, you must first define a ___ that indicates what it contains
class
Create a cat class in python
> > > class Cat:
… pass
We need to say pass to indicate this class is empty
A _____ is a variable inside a class or object
attribute
A _____ is afunction in a class
method
If you want to assign object attributes at creation time, you need the special Python object initialization method
__init__()
> > > class Cat:
… def __init__(self):
… pass
When you define __inti__() in a class definition, its first parameters should be named ___.
self –argument specifies that it refers to the individual object itself
EXAMPLE
> > > class Cat():
… def __init__(self, name):
… self.name = name
…
Create an object from the cat class
> > > furball = Cat(‘Grumpy’)
Function that allows you to check is a class is derived from another class
issubclass()
Child class ____ methods from parnt class
inherits
You can also _____ methods in the parent class
override
The child class can also ____ a metohd that was not present in parent cas
add
We can override any methods including _____
__init__()
Wat if child class wated to call parent method?
super()
Example of using super in the initialize function
> > > class EmailPerson(Person):
… def __init__(self, name, email):
… super().__init__(name)
… self.email = email
Some object-oriented languages support _____ object attributes that can’t be accessed directly from the outside. Programmers then may need to write _____ and _____ methods to read and write the values of such private attributes.
Python doesn’t have private attributes, but you can write getters and setters with obfuscated attribute names to get a little privacy
private, getter, setter
The Pythonic solution for attribute privacy is to use _______
properties
There are two ways to do this. The first way is to add __________________ as the final line of our previous Duck class definition:
name = property(get_name, set_name)
In the second method, you add some decorators and replace the method names get_name and set_name with name
@property, which goes before the getter method
*@name.setter, which goes before the setter method
You can assign attributes to _____, and they’ll be inherited by their child objects
classes
»> class Fruit:
… color = ‘red’
…
»> blueberry = Fruit()
»> Fruit.color
‘red’
»> blueberry.color
‘red’
*If there’s no preceding decorator, it’s an ______ _____, and its first argument should be self to refer to the individual object itself
instance method
*If there’s a preceding @classmethod decorator, it’s a _____ ______, and its first argument should be cls (or anything, just not the reserved word class), referring to the class itself.
class method
*If there’s a preceding @staticmethod decorator, it’s a ______ ______, and its first argument isn’t an object or class
static method
Examples of class method vs instance method
> > > class A():
… count = 0
… def __init__(self):
… A.count += 1
… def exclaim(self):
… print(“I’m an A!”)
… @classmethod
… def kids(cls):
… print(“A has”, cls.count, “little objects.”)
…