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
Python for Beginners: A Complete Beginner's Guide to learning Python quickly by Daniel O'Reilly.epub
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.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9904)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9785)
The Mikado Method by Ola Ellnestam Daniel Brolund(9769)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8282)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7771)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7756)
Grails in Action by Glen Smith Peter Ledbrook(7686)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7550)
Windows APT Warfare by Sheng-Hao Ma(6773)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6499)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6399)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6367)
Kotlin in Action by Dmitry Jemerov(5048)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4313)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3972)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3755)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3699)
The Ultimate iOS Interview Playbook by Avi Tsadok(3675)
