9 - Lecture 8, 14 Nov 2022 [ID:46013]
50 von 723 angezeigt

So, last time we started with object-oriented programming. Are there any immediate questions

from last time? So, we did two things last time, right? We discussed about how to debug

programs and how to do defensive programming, and then we started a little bit with that,

with object-oriented programming. But that is really, this is really a kind of key concept

of modern programming languages. So let's quickly recap. If there are no questions immediate

once, then, I mean, feel free to interrupt me anytime. Let's quickly recap what it's

for. And really, the main purpose is to be able to define our own types. Like we can

have these sort of types which we know already, integers, floating point, strings, whatsoever

you name it, but very often we want to have our own type. And the way to do this is by

writing a class. And really what a class does is nothing else than having an internal data

representation and internal methods to how to work with this data and procedures methods

to interact with this data that is stored in this class. And you have encountered already

a couple of these methods because in Python everything is an object, everything is a class.

So everything you have used so far is basically a function method of functions. It's probably

not the most useful one in the world. So, okay. So we have already used classes a lot

because all of the types we've learned already are classes. So that I've covered last time

already you don't, in Python, so Python is not only a dynamically typed language so you

don't need to always write down what type you're wanting to work with. The interpreter

will do it for you but it will also do garbage collection for you most of the time. You can

forget types and forget variables with the del function but you don't have to, right?

You don't need to. Basically it's kind of good practice if you have large data objects

that occupy a lot of memory. If you don't need them anymore you can delete them but

most of the time you just don't need to destroy your objects. Okay. So this is again motivation

from last time. So we need, that's a kind of reoccurring theme. It's all about data

extraction, right? We want to write down, we don't want to overthink about the nitty-gritty

details of how to work with our data. We'll always have our internal representation and

some sort of interface, methods to interact with this data object. And all of these things

are called attributes, you know? So it can be data attributes or method procedure attributes.

I have discussed last time this list object which is pretty funny example actually for

a program because in Python lists seem so intuitive, right? It's relatively straightforward

and simple to work with them. Just put in your kind of data objects, your types and

then access them with the square brackets. That's basically it. But in reality if you

ever enjoy a kind of more low-level programming course then you will realize that implementing

a list of just the very basic tools I gave you in the very beginning, you remember memory

and pointers and kind of addresses in memory. That's actually not that easy. You need to

build something like a linked list like in this example here where you always have two

elements, the actual data value and the address of the next data value in this list. And you

don't want to expose this really to your user because it's cumbersome, error-prone if you

don't know what you're doing and you probably overwrite the pointer instead of the value

because you would need to work very low-level then, you know, all hell breaks loose. And

your linked list is suddenly not linked anymore but links to somewhere else in the memory

and then you'll get a segmentation forward and good luck. So you want to hide that, yeah?

You want to hide this kind of functionality and just provide something like that indexing

operations that are intuitive to a user of this object. But somebody implemented that

that way, right? So that is something somebody implemented and you can just use out of the

box. And that's really the aim of object-oriented programming that you don't need to think about

that. Okay, you are dividing Conquer into reusability of code is a key concept. I mean,

in theory, if you're a genius, of course, you can write all your operations you need

down in assembly code or somewhere low-level, probably binary and instruct your processor

to do certain things. Even that you probably won't be able to do without reading the instructions

Zugänglich über

Offener Zugang

Dauer

01:23:26 Min

Aufnahmedatum

2022-12-05

Hochgeladen am

2022-12-05 18:46:04

Sprache

en-US

OOP and class inheritance

Tags

Python programming
Einbetten
Wordpress FAU Plugin
iFrame
Teilen