Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OOP #16

Open
howardbaik opened this issue Apr 15, 2024 · 0 comments
Open

OOP #16

howardbaik opened this issue Apr 15, 2024 · 0 comments

Comments

@howardbaik
Copy link
Contributor

howardbaik commented Apr 15, 2024

@cansavvy Below is the OOP section from #10, which was taken out of that PR. See #10 (comment).

Also, I've made a branch dedicated to OOP: 16-oop


Classes and Object Oriented Programming (OOP)

In R, the most widely used unit of composition for code is functions, and in Python, it is classes. Classes are how you organize and find methods in Python. This approach to code composition is called object oriented programming (OOP). Let's dive in the details of OOP.

An object is any entity that you want to store and process data about. Each object is an instance of a class in the computer's memory. A class is a template for creating objects. Creating an object from a class is called instantiation. It has properties and methods (functions for the class).

For example, we could have a class called Person. The properties of this class are what describe this Person class:

  • first_name
  • last_name
  • gender
  • date_of_birth
  • occupatiaon

The methods of this class are the functions for this Person class:

  • walk()
  • run()
  • sleep()
  • eat()

Here is a simple Person class for demonstration purposes.

class Person:
  pass # `pass` means do nothing.

Person
#> <class '__main__.Person'>
type(Person)
#> <class 'type'>

instance = Person()
instance
#> <__main__.Person object at 0x102ba75e0>
type(instance)
#> <class '__main__.Person'>

Like the def statement, the class statement is used to create a Python class. First note the strong naming convention, classes are typically CamelCase, and functions are typically snake_case. After defining Person, you can interact with it, and see that it has type 'type'. Calling instance = Person() creates a new object instance of the class, which has type Person (ignore the main. prefix for now).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

1 participant