Learn Python Fundamental in 30 Days — Day 19 (Classes Part 2)

Attributes

  • Attributes are the variables that belong to a class(can be of any data type)
  • Can be accessed after the “.”

Syntax for creating attributes

self.attribute = something

Special method: It’s used to initialize the attributes of an object

__init__()

The First example look like this

class Cat(object):
def __init__(
self,breed):
self.breed = breed # We are creating attributes here
#Then we are creating instance of Cat
x
= Cat(breed="Persian")
y = Cat(breed="Sphynx")

Let’s break down this code

  • Special method __init__() is called automatically right after the object has been created(initialize some data attributes)
  • Each attribute in a class definition begins with a reference to the instance object. It’s by convention named self(that’s by convention that we use a self keyword,it actually doesn’t have to be self but it’s never not self :-) ). Value is passed during the class instantiation
self.breed = breed
  • When we invoke creation of an instance, this will bind the variables to breed within that instance to the supplied value

Now we have created two instances of the Cat class, with two breed type, to access these attributes

>>> x.breed
‘Persian’
>>> y.breed
‘Sphynx’

NOTE: We don’t have any parenthesis after breed and that’s because it’s an attribute and it doesn’t take any arguments.

While trying to create instance of a class we can’t do it like this

x = Cat()

It will throw this error

Traceback (most recent call last):
File “/Users/plakhera/Downloads/salesforce-spring-2017/loggingcode.py”, line 5, in <module>
x = Cat()
TypeError: __init__() missing 1 required positional argument: ‘breed’

Here self by default is given and it lets the method know we are referencing the cat object.

Now let’s take a look at class object attributes. These class object attributes are the same for any instance of the class. In the below example cat is always a mammal irrespective of their name and breed

>>> class Cat(object):
species = “mammal”
… def __init__(self,breed,name):
… self.breed = breed
… self.name = name
>>> x = Cat(“Persian”,”Luca”)
>>> x.name
‘Luca’
>>> x.breed
‘Persian’
>>> x.species
‘mammal’

NOTE: Class Object attributes is defined outside of any methods in the class. Also by convention we place them first before the init.

Methods

We can think of methods as functions defined inside the class

class Circle(object):
pi = 3.14
    def __init__(self,radius=1):
self.radius = radius
    def area(self):
return
self.radius * self.radius * Circle.pi
    def setRadius(self,radius):
self.radius = radius
    def getRadius(self):
return
self.radius
x = Circle()
x.setRadius(3)
print(x.getRadius())
print(x.area())

Let’s break down the code and see what’s going on

  • We instantiated a circle with default radius of 1
  • We define the method to calculate the area
  • Then we define the method to reset the radius
  • Finally method for getting radius

So this end of Day19, In case if you are facing any issue, this is the link to Python Slack channel https://devops-myworld.slack.com

Please send me your details

  • First name
  • Last name
  • Email address

to devops.everyday.challenge@gmail.com, so that I will add you to this slack channel

HAPPY CODING!!!