Python Basic Memorandum Part 3-About Object Orientation-

Article summary

It is a memorandum that I learned according to References with the intention of becoming able to read Python as much as possible. This is a summary of the points that I was interested in and the points that I found useful compared to Java. Part 1 is here Part 2 is here * This is a fairly basic content. </ strong>

class

The concept of class itself is a recognition that is not so different. An example of how to define a class is given below.

Example

java


class SampleClass {
    //Class processing
}

python


class SampleClass:
    #Class processing

Instantiation

Instantiate a class. The handling is almost the same as Java. ~~ Should ~~

java


SampleClass sClass = new SampleClass();

python


sClass = SampleClass()

attribute

It's like an instance variable in Java (?). You can add attributes to your instance.

python


sClass.attr = "Atori Byuto"
print(sClass.attr)
>>Atori Byuto

Class method definition

When adding an argument to a class method, add self to the first argument.

You can use self to manipulate the instance itself

Example

java


class SampleClass {
    int counter = 0;
    
    int count(int a) {
        counter += a;
    }
}

python


class SampleClass:
    counter = 0;

    def count(self, a):
        self.counter += a

Initialization method

The initialization method uses __init__ (special method).

Example

java


publiv class Human {
    String name;
    String birthday;

    public Human(String name, String birthday) {
        this.name = name;
        this.birthday = birthday;
    }
}

python


class Human:    
    def __init__(self, name = None, birthday = None):
        self.__name = name
        self.__birthday = birthday

Encapsulation

Prefix methods and attributes with __ (two underscores) for encapsulation. Access from the outside can be prohibited by adding __. As a convention, if you prefix a method or attribute with _ (one underscore) It has the meaning (it seems) that "it is used only inside the class, so please do not access it from the outside".

  • It seems that underscores have various meanings when I look it up. Links such as articles when searching in the references are posted.

Addition </ strong> ~~ It seems that Python does not define getters and setters. ~~ It seems that defining methods such as get 〇 〇 and set 〇 〇 is deprecated in Python. (How to access the attributes may be summarized separately) In this article, getters and setters are defined in the above way to make it easier for you to understand compared to Java. Please note. If you don't define underscores, getters, and setters, I'm thinking of learning separately and summarizing them.

Example

java


publiv class Human {
    private String name;
    private String birthday;

    public Human(String name, String birthday) {
        this.name = name;
        this.birthday = birthday;
    }

    public getName() {
        return this.name;
    }

    public getBirthday() {
        return this.birthday;
    }

    public setName(String name) {
        this.name = name;
    }

    public setBirthday(String birthday) {
        this.birthday = birthday;
    }
}

python


class Human:
    def __init__(self, name = None, birthday = None):
        self.__name = name
        self.__birthday = birthday
    
    def getName(self):
        return self.__name

    def getBirthday(self):
        return self.__birthday

    def setName(self, name):
        self.__name = name

    def setBirthday(self, birthday):
        self.__birthday = birthday    

Inheritance

Python is multi-inheritable </ strong>. For multiple inheritance, specify the class name separated by ,.

Example

java


public class SuperMan extends Human {
    private String ability;

    public getAbility() {
        return this.age;
    }

    public setAbility(String ability) {
        this.ability = ability;
    }

python


class SuperMan(Human): #For multiple inheritance SuperMan(Human, Man)Define like
    def getAbility(self):
        return self.__ability

    def setAbility(self, ability = None):
        self.__ability = ability

slot

Special methods that can limit the addition of attributes __slots__ = [attribute name 1, attribute name 2, ...]

Improve memory usage efficiency

python


class Profile:
    __slots__ = ['height', 'weight', 'bloodType']

    def __init__(self, bloodType = None):
        self.bloodType = bloodType

a = Profile('A')
print(a.bloodType)
>>A

#Attributes defined in slots
a.height = 170
a.weight = 60

a.constellation = 'Aries'
>>An error occurs

Property

Built-in functions for easily defining getters and setters. Easily control access to private attributes. property (getter method name, [setter method name])

python


class Human:
    def __init__(self, name = None):
        self.__name = name
    
    def getName(self):
        return self.__name

    def setName(self, name):
        self.__name = name

    propName = property(getName, setName)

#When substitution is performed, the setter set in property works.
propName = 'takeshi'
#When fetching, the setter set in property works
print(propName)
>>takeshi

Afterword

When I looked into the disadvantages of Python, I found that it was slow to run. I felt that the __slots__ summarized this time is necessary to improve the disadvantages. I would like to advance my understanding so that I can properly use Python-like functions.

References

Reference book </ strong> Jun Shibata (2016) "Minna no Python 4th Edition" SB Creative Co., Ltd.

Official Reference </ strong> Python Official Reference

About data types </ strong> Python built-in data type classification table (mutable, etc.)

Underscore </ strong> Python course for beginners, object-oriented 7 encapsulation Lesson 12 Class Definition-Introduction to Python Basic Grammar python underscore this isn't it

Recommended Posts