Introduction to python class methods and object methods

高洛峰
Release: 2017-03-07 15:46:42
Original
1389 people have browsed it

This article is mainly about learning python class methods and object methods with everyone, starting from a simple example. Interested friends can refer to

The examples in this article are for python class methods and objects Method for study and research, the specific content is as follows

class Test_Demo:
  TEST = 'test_value'

  def __init__(self,name,age):
    self.name = name
    self.age = age
  #static method
  @staticmethod
  def test_static():
    return Test_Demo.TEST
  #特性
  @property
  def test_property(self):
    return self.name+':'+str(self.age)
  #类方法
  @classmethod
  def test_class(self):
    return self.TEST

if __name__ == '__main__':
  test_demo = Test_Demo('zj',23)
  #print(test_demo.name)
  print(Test_Demo.test_static())
  print(test_demo.test_property)
  print(test_demo.test_class())
Copy after login

Output results:

Introduction to python class methods and object methods

Note: The difference from PHP is:

Class methods and static methods can access the static variables of the class (class variables, TEST), but they cannot access instance variables (i.e. name, age)

If accessed An error will be reported:

Introduction to python class methods and object methods

Is the above the entire content of this article? I hope it will be helpful to everyone's study.

For more articles related to the introduction of python class methods and object methods, please pay attention to 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