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

visit

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