atlasgcx
(Chenxi Ge)
1
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
arpita
(Arpita Jena)
2
Some more examples of OOP in python
jeremy
(Jeremy Howard)
3
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)
timlee
(Tim Lee)
4
Also a great website with a simple walk through
https://python.swaroopch.com/oop.html
1 Like