Home > Backend Development > Python Tutorial > How to call class method in python

How to call class method in python

silencement
Release: 2019-06-13 15:59:13
Original
5008 people have browsed it

How to call class method in python

Classes in python are used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes.

If you want to call a method in a class, you must first define a class. To define a class in python, use the class keyword

class Person():
    def __init__(self,name,age):#__init__()方法为类的构造方法
        self.name = name 
        self.age = age
    def detail(self):(通过self调用被封装的内容)
        print(self.name)
        print(self.age)
obj1 = Person('santos',18)
obj1.detail() #python将obj1传给self参数
Copy after login

There are two ways for python to call methods in a class: direct call through an object and indirect calls via self.

The above is the detailed content of How to call class method in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template