Object and Classes Flashcards

(37 cards)

1
Q

To create an object, you must first define a ___ that indicates what it contains

A

class

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

Create a cat class in python

A

> > > class Cat:
… pass

We need to say pass to indicate this class is empty

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

A _____ is a variable inside a class or object

A

attribute

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

A _____ is afunction in a class

A

method

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

If you want to assign object attributes at creation time, you need the special Python object initialization method

A

__init__()

> > > class Cat:
… def __init__(self):
… pass

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

When you define __inti__() in a class definition, its first parameters should be named ___.

A

self –argument specifies that it refers to the individual object itself

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

EXAMPLE

A

> > > class Cat():
… def __init__(self, name):
… self.name = name

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

Create an object from the cat class

A

> > > furball = Cat(‘Grumpy’)

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

Function that allows you to check is a class is derived from another class

A

issubclass()

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

Child class ____ methods from parnt class

A

inherits

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

You can also _____ methods in the parent class

A

override

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

The child class can also ____ a metohd that was not present in parent cas

A

add

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

We can override any methods including _____

A

__init__()

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

Wat if child class wated to call parent method?

A

super()

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

Example of using super in the initialize function

A

> > > class EmailPerson(Person):
… def __init__(self, name, email):
… super().__init__(name)
… self.email = email

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

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

A

private, getter, setter

17
Q

The Pythonic solution for attribute privacy is to use _______

18
Q

There are two ways to do this. The first way is to add __________________ as the final line of our previous Duck class definition:

A

name = property(get_name, set_name)

19
Q

In the second method, you add some decorators and replace the method names get_name and set_name with name

A

@property, which goes before the getter method
*@name.setter, which goes before the setter method

20
Q

You can assign attributes to _____, and they’ll be inherited by their child objects

A

classes
»> class Fruit:
… color = ‘red’

»> blueberry = Fruit()
»> Fruit.color
‘red’
»> blueberry.color
‘red’

21
Q

*If there’s no preceding decorator, it’s an ______ _____, and its first argument should be self to refer to the individual object itself

A

instance method

22
Q

*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.

23
Q

*If there’s a preceding @staticmethod decorator, it’s a ______ ______, and its first argument isn’t an object or class

A

static method

24
Q

Examples of class method vs instance method

A

> > > 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.”)

25
The first parameter of an instance method is ____, and Python passes the object to the method when you call it
self
26
__eq__(self,other)
self == other
27
__ne__(self,other)
self != other
28
__str__(self)
str(self)
29
__len__(self)
len(self)
30
Aggregation example
>>> class Bill(): ... def __init__(self, description): ... self.description = description ... >>> class Tail(): ... def __init__(self, length): ... self.length = length ... >>> class Duck(): ... def __init__(self, bill, tail): ... self.bill = bill ... self.tail = tail ... def about(self): ... print('This duck has a', self.bill.description, ... 'bill and a', self.tail.length, 'tail') ... >>> a_tail = Tail('long') >>> a_bill = Bill('wide orange') >>> duck = Duck(a_bill, a_tail) >>> duck.about()
31
For example, if duck has tail and bill, and we create classes Tail and Bill, we can make tail and bill objects and provie them to the new ___ object
duck
32
A named tuple is a subclass of tuples with which you can access values by ____ (with .name) as well as by position (with [ offset ]).
name
33
how to import named tuple
>>> from collections import namedtuple
34
Creating a named tuple
>>> from collections import namedtuple >>> Duck = namedtuple('Duck', 'bill tail') >>> duck = Duck('wide orange', 'long') >>> duck Duck(bill='wide orange', tail='long') >>> duck.bill 'wide orange' >>> duck.tail 'long'
35
Creating a named tuple from dictionary
>>> parts = {'bill': 'wide orange', 'tail': 'long'} >>> duck2 = Duck(**parts) >>> duck2 Duck(bill='wide orange', tail='long')
36
Names tuples are _____, bu you can replace one or more fileds and return another named tuple
immutable
37
Examples of data classes
>> class TeenyClass(): ... def __init__(self, name): ... self.name = name ... >>> teeny = TeenyClass('itsy') >>> teeny.name 'itsy' Doing the same with a dataclass looks a little different:>>> from dataclasses import dataclass >>> @dataclass ... class TeenyDataClass: ... name: str ... >>> teeny = TeenyDataClass('bitsy') >>> teeny.name 'bitsy'