Class and instance attributes

class Foo(object):
def __init__(self, lst):
self.lst = lst
def sum(self):
self.sum = sum(self.lst)
return self.sum
f = Foo([1,2,3])print(f.sum())
print(f.sum())

Python Classes and Methods

  1. Define what is a class
  2. Describe how to create a class
  3. Define what is a method
  4. Describe how to do object instantiation
  5. Describe how to create instance attributes in Python

What is a class?

Instance = class(arguments)

How to create a class

>>> class Snake:
... pass
...
>>> snake = Snake()
>>> print(snake)
<__main__.Snake object at 0x7f315c573550>

Attributes and Methods in class:

Attributes:

>>> class Snake:
... name = "python" # set an attribute `name` of the class
...
>>> # instantiate the class Snake and assign it to variable snake
>>> snake = Snake()
>>> # access the class attribute name inside the class Snake.
>>> print(snake.name)
python

Methods

>>> class Snake:
... name = "python"
...
... def change_name(self, new_name): # note that the first argument is self
... self.name = new_name # access the class attribute with the self keyword
...
>>> # instantiate the class
>>> snake = Snake()
>>> # print the current object name
>>> print(snake.name)
python
>>> # change the name using the change_name method
>>> snake.change_name("anaconda")
>>> print(snake.name)
anaconda

Instance attributes in python and the init method

class Snake:    def __init__(self, name):
self.name = name
def change_name(self, new_name):
self.name = new_name
>>> # two variables are instantiated
>>> python = Snake("python")
>>> anaconda = Snake("anaconda")
>>> # print the names of the two variables
>>> print(python.name)
python
>>> print(anaconda.name)
anaconda

--

--

--

Web Developer graduated from Holberton School. With experience in as C, Python, NodeJS, React, React Native, HTML, CSS, Boostrap and Flexbox

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Codility Algorithm Practice Lesson 5: Prefix Sums, Task 3: Genomic Range Query— a Python approach

Enlightening Math

How to get pricing tier information of Azure SQL database

The End of Season 1 is here

How to solve every(most of the time) Wordle problem

How to solve every(most of the time) wordle problem

Setting up an Amazon AWS Aeon Public node on a FREE EC2 Instance

Oracle — Pass Value to IN query in Procedure | Code Factory

Shaping Resilient Initiatives With Purpose-to-Practice

NFT Merge is Release

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrés Felipe García Rendón

Andrés Felipe García Rendón

Web Developer graduated from Holberton School. With experience in as C, Python, NodeJS, React, React Native, HTML, CSS, Boostrap and Flexbox

More from Medium

Static Libraries in C

Programming Diaries — Day 1 — Starting C#

Strings and Integers in C++

Briefly The Importance Of Interfaces