OOP in one minute

define a class

class fruit():
    def __init__(self, size, color):
        self.color = color
        self.size = size
    def grow(self, increment):
        self.size = self.size + increment
        return(self.size)

create an object of that class

apple = fruit(10, 'red')

access attributes

apple.size
10
apple.color
'red'

use methods

apple.grow(10)
20
4 Likes

Some more examples of OOP in python

Thanks @atlasgcx . To make your code more readable, try formatting it like this:

Input:

 ```python
class fruit():
    def __ init __(self, size, color):
        self.color = color
        self.size = size
    def grow(self, increment):
        self.size = self.size + increment
        return(self.size)
```

Result:

class fruit():
    def __ init __(self, size, color):
        self.color = color
        self.size = size
    def grow(self, increment):
        self.size = self.size + increment
        return(self.size)

Also a great website with a simple walk through

https://python.swaroopch.com/oop.html

1 Like

Thanks Jeremy!

Try this…Object Oriented Programming FAQ