Python for Beginners: A Complete Beginner's Guide to learning Python quickly by Daniel O'Reilly

Python for Beginners: A Complete Beginner's Guide to learning Python quickly by Daniel O'Reilly

Author:Daniel O'Reilly [O'Reilly, Daniel]
Language: eng
Format: azw3, epub
Published: 2020-11-11T00:00:00+00:00


CHAPTER 7:

Classes in Python

O bjects are often based on real-life objects, or they can be special objects created for your program. It all depends on what problem you are solving. First, let’s look at the basic syntax for a class.

class ClassName:

‘optional class documentation string’

class_suite

Class_suite represents the methods and variables in the class. The class documentation string can tell you what the class is.

Let’s look at an example class. This class represents a person.

class Person:

‘This class represents a person.’

name = “John Doe”

age = 20

def displayName():

print “My name is “ + self.name

def displayAge():

print “My name is “ + self.age

def changeName(self, newName):

self.name = newName

def changeAge(self, changedAge):

self.age = changedAge

This class represents a person and has a name and age. The functions, otherwise known as methods, in the class initialize the class, print the name, print the age, change the name, and change the age. These are often known as “getters” and “setters,” which change the attributes of the object. The attributes, in this case, are the name and age of the person.

The first method __init__() is a special method. This is also known as the class constructor, which can be used to create a new object and pass in values for the attributes. In this case, you pass in values for the name and age that you want the person to have. If there aren’t any values, then the defaults “John Doe” and age 20 are used.

The other class methods you want to use can be declared just like regular functions. The only difference is that the first argument is always self, which represents the current instance of the object. There is no need to include it when you call the methods, though. Python adds that for you. Let’s look at an example of creating an object and calling these methods.

person1 = Person(“John Smith”, 30)

This creates a new person named John Smith who is 30-years-old.

person1.displayName()

person1.displayAge()

This will print the name and the age.

person1.changeName(“Jane Smith”)

person1.changeAge(31)

This will change the person’s name to Jane Smith and make the age 31.

You can also add, modify, and delete attributes to an object at any time.

Let’s add an occupation attribute to the person above.

person1.occupation = “Farmer”

Now, we have made person1 a farmer.

person1.occupation = “Retired”

Now, we have changed the person’s occupation to “Retired.”

You can use the following functions to modify attributes as well.

getattr(object_name, attribute_name)

This will return the value of attribute_name for object_name.

hasattr(object_name, attribute_name).

This will return true if object_name has attribute_name and false otherwise.

setattr(object_name, attribute_name, attribute_value)

This will set the attribute_name attribute to attribute_value for object_name. If the attribute doesn’t exist, it will be created for you.

delattr(object_name, attribute_name)

This will delete the attribute_name attribute for object_name.

Let’s look at an example with our person 1 object.

getattr(person1, name) will return Jane Smith because that is the current value for the name.

hasattr(person1, age) will return true because the person has an age.

hasattr(person1,degree) will return false because the person doesn’t have a degree attribute.

setattr(person1, degree, “Bachelors”) will create the attribute degree and set the value to “Bachelor’s.”

setattr(person1, age, 35) will change Jane Smith’s age to 35.

delattr(person1, degree) will delete Jane Smith’s degre atrribute.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.