[PYTHON] Articles for guitarists and bassists to fully understand object orientation

Articles for guitarists and bassists to fully understand object orientation

Introduction

Nice to meet you, my name is Sotoku. This is Qiita's first post. I usually live on Twitter and write Python etc. nicely. By the way, this article is an object-oriented commentary article for guitarists and bassists </ b> as the title suggests. If you have only one experience, you may be wondering "What?", But in fact, there is something very easy to understand in common between playing guitar and bass and object-oriented programming, so I will explain it this time. I wish I could go. Also, this article does not fully understand *** object-oriented ***, title scams, and I sincerely regret it. The purpose is to explain in an easy-to-understand manner for beginners, and because I am not in a position to fully understand myself, the article may be full of mistakes, but in that case I would appreciate it if you could kindly point out. is.

Who is the target of this article

  • Guitarist
  • Bassist
  • People who don't understand object orientation at all

People not covered by this article

  • People who don't understand musical instruments at all
  • Someone who has a complete understanding of object orientation

What is object-oriented?

According to wikipedia

Object-orientation (object-orientation) is one of the software engineering concepts, and is a concept or concept used in software design and program description.

... apparently ... As you can see here, object-oriented programming is roughly like the idea of programming. As we will see later, this idea is often very useful in designing and team development. Also, what is called an object-oriented language, especially Java, is a very object-oriented language, and it often fails to be implemented without some understanding. (It's often forgotten that Python is also an object-oriented language, I'm mainly writing Python, so I'll use Python when explaining it in code in this article.) Specifically, the concept is roughly to implement the actual situation that achieves a certain purpose </ b> by dividing it into multiple objects </ b> to achieve the purpose. I understand For example, when implementing a calculator application, it is decomposed individually into an object that receives input, an object that outputs calculation results from input (this is also further divided depending on the desired calculation content), and an object that returns output. The image is to implement it and put it together at the end.

Common points with musical instruments

After reading this far, you might think that the guitarists and bassists somehow resemble that </ b>. Yes, that </ b> that </ b>. *** Effector ***. As an effector, amp, guitar, or guitarist, you can think of yourself as an object with independent roles </ b>, such as sound processing, input, and output. Now, let's actually write your effect board in an object-oriented manner to make it easier to understand. (The code below doesn't really work, it's a demo to make it easier to understand.)

Let's say your guitar is Mustang and your effects are Bruce Driver and Metal Zone. The amp is Jazz Chorus. Let's say you want to use these objects to finally implement a feature that "returns a nice output for the input sound". In object orientation, write like this.

Sound.py


#Guitar class
class Musutang:
    @staticmethod
    def pickup('''Something pulled sound'''):
        #Processing to amplify and output sound
        return cleansound #Clean tone

#Basic class of effectors
class Effector:
    #Write the basic structure of the effector
    @staticmethod
    def Efect('''Incoming sound'''):
        #Basic processing of effectors

#A class that takes over the effector class
class BluesDriver(Effector):
    #Override
    @staticmethod
    def Effect('''Incoming sound'''):
        #Processing to make the sound cool
        return bluessound

class MetalZone(Effector):
    #Override
    @staticmethod
    def Effect('''Incoming sound'''):
        #Processing to transform sound
        return metalsound

#Amp class
class amp:
    @staticmethod
    def JazzChorus('''Incoming sound'''):
        #Processing to amplify and output sound
        return mixedsound #The last sound coming out of the amp


#Method to actually combine and move classes
def main():
    sound = input()
    clean = Musutang.pickup(sound)
    blues = BlusesDriver.Effect(clean)
    metal = MetalZone.Effect(blues)
    mixed = amp.JazzChorus(metal)

    return mixed

#Magic
if __name__ == '__main__':
    main()

It's a good code with no coding conventions or crap, but I think you can get a feel for it. When actually implementing it, write another function in another file and call it using import etc.

What makes me happy

Object-oriented allows you to assign experts by function, for example when implementing. In the case of a guitar, you often don't know the internal structure of an effector or amp. But you have the ability to play the guitar (give instructions). On the contrary, the creator of the effector does not necessarily have to be a guitarist, but a professional circuit. In this way, if everyone creates only functions that they can create </ b> without knowing each other's areas of expertise, an effector board or system that can finally produce good sound is completed. That is considered to be an object-oriented advantage. There are many other benefits, such as better visibility of the entire code and easier additions and extensions. Why don't you, a guitarist, take this opportunity to start object-oriented programming?