Hey, welcome to a new video on p.
So far, we created two classes in our event Scraping app, an event class and an email class.
Now we have two more functions which are floating around without a glass.
So since we decided to make this an app, then it's a good idea to place all the functions inside classes.
So what do you think should be the next class we should have here?
So we have a store function and a read function.
These two functions are working on a database, so this stores data in the database and this other function
reads data from the database.
So I think that these two should belong to a database class.
So this is just the name I decided to give to this class.
You can also call this database table or another name, so you should try to give your classes meaningful
names.
I'll just leave it database.
Make some break lines in here.
Add the self arguments to each of these.
I'll cover today what self is.
Now this class here is a bit more demanding that the other two classes.
This class is also using this connection object in here.
So first we establish a connection here and we store that connection in this variable, and then this
class is using that variable or both.
It's a better idea to have that.
Expression inside the class.
Since we are using classes now to organize our code, then what I'll do.
I'll just cut this out from here.
And where do we place that?
So we have two methods the store methods and the read methods, and both methods use that connection
variable.
So one way is to have that expression in both methods here and here.
Or both.
This is not the recommended way to do this.
We should place this expression somewhere where it is accessed by the two methods.
So just once, instead of having repetitive codes like this, so I'll delete it from here and here and
I'll add a special underscore underscore in it, underscore, underscore methods.
So make sure that the method is named exactly like this underscore, underscore in it in lowercase letters,
underscore, underscore.
And this also has self argument as well.
And the column and then the body of this method will be that expression connection is equal to you light
that's connect, dot, dot, dot, db.
Now this variable then should be accessed by the store and the reads functions.
Currently, though, PA charm is giving us a clue that these methods do not have access yet to this
connection variable.
You see, it's underlined in red.
It says unresolved reference connection.
So basically this function, this method doesn't see that variable.
To make that variable visible to these two methods.
What we do is we add self dot in front.
So now connection is a property of the self argument.
Then in here we do the same self dot connection, self, dot connection.
So now we point to that property to that variable, which is a property of self.
And so self is like a special variable and all the methods have access to self, right?
So we use self as the middleman to access the variables.
So self dot connection, cell, dot connection.
Same goes in here, self dot connection, and so on.
And then when we call the the class here, down here we say if extracted and so on, we create a database
instance using the database class, which is with a capital D, right.
This class.
And then in here we see a database with lowercase letters that reads and same here, database that store.
And if you run the code now, it will work the same as it did before.
Now, perhaps your data DB file has some data already, so make sure that you have an empty data DB
file.
Perhaps you need to redownload it from the lecture resources in the previous video so that you have
a fresh empty database DB file and that will send you an email.
If that's the case, when a tool is found, an event is found.
But I guarantee you that this code works the same as it used to do when it had functions in here.
Now, if you remember from the previous apps where we used to create PDF reports, we used to have.
This line of code pdf is equal to f pdf where f pdf was a class.
So a class which created PDF instances and this class got some arguments such as an orientation argument
which was P or L or portrayed something like that.
So this class used to get arguments of both our classes here database, event and email.
They don't get any arguments, but we can also have them make it possible that they get arguments.
So let's do that with this database class and then I'll explain you what self is and what in its method
is.
Exactly.
So if you want this database class to get an arguments, you want to add that as a parameter of the
init function or the init method.
So the parameter could be, for example, database path.
Right.
And so this goes in here as well.
Database path.
Right.
And then when you call the class here, let me fold this function.
And this one in here.
So I'm not deleting them.
I'm just making them invisible for now.
So the code is still there, right?
I just want to do that.
So you see the class and also these lines in here.
So now in database, we say database path is equal to the value, which is the string data DB which
points to this file.
So now this class is getting an argument.
So that's the argument and that's the value.
So the way you give arguments to a class is by providing a parameter in the in its methods.
So what is in it and what is self?
Let me explain.
These two very delicate concepts now in it is a special method, as I told you already.
Why is it special?
Well, it is special because this method is executed when you create an instance of the class.
So specifically, when the interpreter runs this line here, this init method is called all the background.
So perhaps if you had like a print function here.
Hello.
And you execute this code that hello will be printed out when the interpreter reaches this line here.
Right.
And you'll get hello in the command line.
So I can also prove you that by opening the console here.
Class food.
Just a dummy class.
Define an init method with self and not other arguments, but just print out hello like that.
And then another method.
Define do something with self.
Print.
I am the do method.
Execute creates a new instance.
Fu one.
Equal to fu.
When I execute that now hello will be printed out because this line here, as I mentioned, executes
that method.
But this other method is only executed when you point to foo one event to that method explicitly.
So execute that and you'll get.
I'm the do methods.
Now what is self?
Self is a special argument.
So we have a special method and we have a special argument.
Self.
Self actually is the variable which holds the instance that is being created.
So for example.
In the food class here.
This is an instance, right?
And we can have multiple instances such as food.
Two is equal to foo.
Great.
So we have a few one instance.
We have a few.
For instance, you'll see that these have different addresses in memory.
So you see that this has this address.
This has another address.
These are just random IDs that are assigned by Python.
So but I'm trying to tell you that these are like different objects, different instances.
And so self is like the variable which holds the instance inside the class.
And so when you say self, that connection, you are pointing to the connection of that particular instance.
Let me try to explain it better.
I know this is confusing, so let's create another class.
I'll use an arrow key to call this previously executed statements.
So the class.
But this time I'll go up here and I'll add a new argument.
Let's say X, right?
And then I'll say self that x is equal to x.
Execute.
Now FU three is equal to FU with X being one and FU four is equal to FU again with x being two.
Know fu three dot x gives you one while fu for that ex gives you two.
So that proves my point that foo three and foo four are different instances.
So you can try to imagine this like being the self argument.
But outside the class it's not called self.
It's called the variable name which holds that instance.
But inside the class it's self.
So basically foo three dot x is self, dot x.
So when the user gave the x value to x here, that value went in here first.
So x is equal to one, right?
And then self does x is equal to one.
So this x here is that x, right.
So one is assigned to self that x.
So that's the property of self and that's just an argument.
It's a bit confusing, but that's why we have applications and you'll see how you'll use self in different
scenarios and that will become second nature to you.
But through now.
This is what you should remember.
So let's make a revision of what we did in this video.
So you learn the init method which is executed when you instantiate the class, when you create an instance
of the class.
So init is executed in here, then we have self, which is a variable inside that class and it holds
the instance that the user is creating.
And then we have other methods such as tool and reads.
And then we have properties such as connection here or this X here, right?
That's a property, that's a method and that's what you should remember for now.
So these are concepts that we will revise again using different examples.
So don't worry if you don't get things right away and I'll talk to you in the next videos.