Python Classes and Object Creation (OOP) > Python

Entire search within the site

Python

Python Classes and Object Creation (OOP)

Page Info

Content

Python Classes and Object Creation (OOP)

In Python, Object-Oriented Programming (OOP) allows you to create classes, which are blueprints for objects. Objects are instances of classes, containing attributes (variables) and methods (functions) that define their behavior.

Defining a Class and Creating Objects


# Define a class
class Person:
    # Constructor (__init__) to initialize object attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Method to display info
    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

# Create objects (instances) of the class
person1 = Person("Alice", 25)
person2 = Person("Bob", 30)

# Access object methods
person1.greet()  # Output: Hello, my name is Alice and I am 25 years old.
person2.greet()  # Output: Hello, my name is Bob and I am 30 years old.
  

Explanation:

  • class Person: Defines a class named Person.
  • __init__(self, ...): Constructor method that initializes object attributes.
  • self.name and self.age: Instance variables unique to each object.
  • person1 = Person("Alice", 25): Creates an object person1 of class Person.
  • person1.greet(): Calls the method defined in the class for the object.

Adding More Methods


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        print(f"Hello, my name is {self.name}.")

    def have_birthday(self):
        self.age += 1
        print(f"Happy Birthday! {self.name} is now {self.age} years old.")

# Create an object
person = Person("Charlie", 20)
person.greet()          # Output: Hello, my name is Charlie.
person.have_birthday()  # Output: Happy Birthday! Charlie is now 21 years old.
  

Key Points

  • Class: Blueprint for creating objects with attributes and methods.
  • Object: Instance of a class.
  • Constructor (__init__): Initializes object attributes.
  • Methods: Functions defined in a class that operate on objects.
  • OOP allows code reuse, modular design, and better structure in larger programs.

SEO Keywords

Python class example, Python object creation, Python OOP, Python __init__ method, Python instance methods, Python object-oriented programming

Using classes and objects in Python allows you to write modular, reusable, and organized code, which is essential for building scalable applications.

Good0 Bad0

댓글목록

등록된 댓글이 없습니다.

Python

Latest Posts

Total 31 posts | Page 1
Python List
Title-Author-data
Python Regular Expressions (RegEx) Basic Matching 12
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Timer and Time Measurement (time module) 9
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Iterators (Iterator Usage) 10
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Inheritance (OOP) 6
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Classes and Object Creation (OOP) 9
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Dictionary Sorting (By Key / By Value) 5
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Logging Implementation 5
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Exception Handling (try-except) 8
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python CSV File Reading and Processing 7
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python JSON Data Handling (Read/Write) 6
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python File Reading and Writing (Text Files) 4
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Decorators: Implementation and Usage 11
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Generators: Implementation and Usage 7
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Lambda Functions 9
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Remove Duplicate Elements from a List 11
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New

visit

today
58
yesday
144
maxday
144
allday
202
Copyright © https://goposu.com/g All rights reserved.