Conditional structures - Loops - Classes

 One of the fairly common scenarios you'll run into in programming is making decisions. Your program needs to compare values and execute code based upon some logical outcome. And that's where conditional logic comes into play. In Python, conditionals are handled by the "if", "elif", "else" statement.


- Repeating code over and over again, via a construct known as a loop, is also a fairly common scenario in programming. Python provides a couple of ways of doing that. Loops in Python work a little bit differently than you might be used to in other languages. For example, in JavaScript you have the concept of an index counter, that controls the number of iterations in a for loop. Alright, that's a brief look at loops in Python.


- Just like other languages, such as JavaScript, Python gives you the option of writing code that is procedural or objected oriented. So, that you can use classes in Python. Classes are a really great way of encapsulating functionality that can be kept together and passed around as a complete module for use in other projects and that's what we're going to take a look at now.  I'm not going to add code to the main function, just yet. 

#
# Example file for working with classes
#

I'm going to start by defining a class. So, let me just add some code, here and I'll define some methods and I'll explain all of this in a moment and this one will take an argument. Classes are defined using the class keyword and they're given a name and if this class was based on a superclass that I was inheriting from, I would put the name of that other class, here inside these parentheses. But this is just a simple base class that doesn't depend on anything else, so, I don't have to put anything in there. Inside a class, I can define functions or in object oriented terminology, they are called methods and they are part of this class. So, I indent my code and then I use the def keyword just like I would for any other function and in this case, I'm calling it method one and I have another one called method two. Usually, the first argument to any of the methods of a class, is the self argument and the self argument refers to the object itself. It's kind of like this keyword in JavaScript. So, inside this method, self will refer to this particular instance of the object that's being operated on. So, down here, in the main function, I'll instantiate the class by writing C equals myClass with parentheses and that will instantiate an object instance of this class. So, once I've got the class created, I can then call methods in the class like I can call methods on any other object. 

class myClass():
    def method1(self):
        print("myClass method1")

    def method2(self, someString):
        print("myClass method2: " + someString)

So, I'll write C.method1 and then I can write C.method2 and I'll give it an argument. This is a string. Now, notice that when I call the class methods I don't have to supply the self keyword. That's automatically handled for me by the Python run time. I just have to care about the arguments that I'm passing in to each of the methods. Let's save then and let's run it. So, go to my debug view and remember, you can run this in your terminal if you're not using VS code. So, I'll click run and I'll show the output window and I'll run it and you can see here, inside method one, we're printing out myclass method one and that's this result and then, myclass method two plus whatever the argument that was passed in. So, that's this is a string, right here. That's the output that we're getting. 

def main():
    c = myClass()
    c.method1()
    c.method2("This is a string")
    c2 = anotherClass()
    c2.method1()

Now, let's take a look at another feature of object oriented programming. This time, we're going to create another class. So, let's do that. So, I copy this one and paste it and I'm going to rename it another class and this time it's going to inherit from myclass. So, I'm going to put myClass inside the parentheses, right here and then, inside method one, which just takes self keyword, I'll print another class method one and then, in method two, I'll print another class method two but I won't use the argument in this case. So, now, I've got two classes.

if __name__ == "__main__":
    main()

Nhận xét

Bài đăng phổ biến từ blog này

Convert your data: Indexes and ratios (part one)

Using the date, time, and datetime classes