...continued
To access attributes or call methods on an object we use a dot-notation., the variable followed by dot and the attribute or the method name.
Syntax: the dot-notation
Example: simplified computer object
#oop #PythonProgramming #DotNotation
To access attributes or call methods on an object we use a dot-notation., the variable followed by dot and the attribute or the method name.
Syntax: the dot-notation
# Accessing an attribute
object.attribute
# Calling a method
object.method()
Example: simplified computer object
class Computer:
# __init__ is a special method (a constructor)
def __init__(self, brand, model):
# attributes
self.brand = brand
self.model = model
# methods
def powerOn(self):
print(f"Powering on {self.brand} {self.model}")
def powerOff(self):
print(f"Powering off {self.brand} {self.model}")
c1 = Computer("HP", "OMEN")
c2 = Computer("Dell", "XPS")
# Test code
print(c2.brand + " " + c2.model)
c1.powerOn()
# Output
Dell XPS
Powering on HP OMEN
#oop #PythonProgramming #DotNotation
β€1
In Python's object-oriented programming, what are the official terms used to describe an object's data and its actions?
Anonymous Quiz
46%
Variables and Functions
14%
Fields and Actions
32%
Attributes and Methods
8%
Properties and Routines
Please open Telegram to view this post
VIEW IN TELEGRAM
Discover a Smarter Way to Learn & Share Knowledge
As a ScholarXiv Ambassador, Iβm excited to introduce you to ScholarXiv β a platform built for students, researchers, academics, and lifelong learners who want to discover and share scholarly knowledge.
Whether youβre working on an assignment, research project, thesis, or simply exploring a new field, ScholarXiv can help you connect with academic resources and ideas.
π¬ Research. Learn. Share. Connect.
If youβre serious about your academic journey, take a look at ScholarXiv and see what it has to offer.
π Explore: scholarxiv.com
As a ScholarXiv Ambassador, Iβm excited to introduce you to ScholarXiv β a platform built for students, researchers, academics, and lifelong learners who want to discover and share scholarly knowledge.
Whether youβre working on an assignment, research project, thesis, or simply exploring a new field, ScholarXiv can help you connect with academic resources and ideas.
π¬ Research. Learn. Share. Connect.
If youβre serious about your academic journey, take a look at ScholarXiv and see what it has to offer.
π Explore: scholarxiv.com
β€2