How to write a Python class


While studying python, I couldn't catch up with how to write a class at once, so I output it and fix it.

Classes and instances

--Class: A recipe for creating "things" in programming

--Instance: "Things" generated based on the recipe

How to declare a class

To work with a class, you need to declare it.

#Class declaration
class SampleClass():

How to use the class

Functions can be defined in the class. A function defined in a class is called a ** method **.

The method definition method is the same as a normal function, but self is used as the first argument.

#Class declaration
class SampleClass():
    def hello(self):
        print("Hello World!")

Instance variable generation

Various information can be defined in the instance. Let's take an example of an item that is likely to be sold at a greengrocer. The following program outputs the name of the home appliance. You can use instance variables by setting "instance.instance variable name".

#Class declaration
class YaoyaItem():
    def vegetable(self):

#Assign the value of YaoyaItem class
yaoyaitem1 = YaoyaItem()
#Added information that the name of yaoyaitem1 is "radish" = "Radish"

#Call the method defined in the class

The vegetable method is called and the following result is obtained


When the method has arguments

If the method has arguments, write the argument names after the second argument.

#Class declaration
class YaoyaItem():
    def payment(self,count):
        #Set the total amount in the return value
        return total_price = self.price * count

#Assign the value of YaoyaItem class
yaoyaitem1 = YaoyaItem() = "Apple"
yaoyaitem1.price = 50

#Substitute 4 for count of the second argument
result = yaoyaitem1.payment(4)
print("I bought 4 pieces. total"+str(result)+It's a yen)

The result is displayed as below.

I bought 4 apples. 200 yen in total


I wrote it for the time being, but I wonder if it has taken root a little.

Recommended Posts

How to write a Python class
[Python] How to make a class iterable
How to write a list / dictionary type of Python3
[Python] How to write a docstring that conforms to PEP8
How to use the __call__ method in a Python class
How to write a metaclass that supports both python2 and python3
[Introduction to Python] How to use class in Python?
Qiita (1) How to write a code name
[Python] How to convert a 2D list to a 1D list
How to write Python document comments (Docstrings)
[Python] How to invert a character string
How to get a stacktrace in python
How to use __slots__ in Python class
How to write Ruby to_s in Python
How to run a Maya Python script
How to install Python
How to install python
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to open a web browser from python
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python
How to generate a Python object from JSON
How to add a Python module search path
Why does Python have to write a colon?
How to write a ShellScript Bash for statement
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to write a named tuple document in 2020
[Go] How to write or call a function
I want to write to a file with Python
How to write a ShellScript bash case statement
How to write a string when there are multiple lines in python
[Introduction to Python] How to write a character string with the format function
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
A road to intermediate Python
XPath Basics (2) -How to write XPath
[Python] How to use list 1
How to call a function
How to update Python Tkinter to 8.6
How to use Python argparse
How to hack a terminal
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
Write to csv with Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
20th Offline Real-time How to Write Problems in Python
How to convert / restore a string with [] in python
How to write a GUI using the maya command