python object oriented programmng code example
Example 1: oop in python
class Object:
#__init__() is called when you create an Object class
def __init__(self,arg):
#stores argument in self
self.arg = arg
def printArg(self):
#calls argument from self
print(self.arg)
Example 2: python how to use oop
#Object oriented programming is when you create your own custom class.
#One reason you should do this is that is saves you time.
#Another reason is it makes calling certain functions easier with tkinter
class Dog:
#init creates certain parameters that allow you to define information quickly.
def __init__(self, name):
self.name = name
def get_name(self):
return self.name
if __name__ == "__main__":
d = Dog(str(input("name your dog: "))
print(d.get_name())
Example 3: class variable in python
# Class variables refer to variables that are made within a class.
# It is generated when you define the class.
# It's shared with all the instance of that class.
# Example:
class some_variable_holder(object):
var = "This variable is created inside the class some_variable_holder()."
def somefunc(self):
print("Random function ran.")
thing = some_variable_holder()
another_thing = some_variable_holder()
# Both does the same thing because the same variable has been passed on from the class.
thing.var
another_thing.var