class methods
1. Class-specific methods
When a class is created, it will contain some methods, mainly the following methods:
Proprietary methods of the class :
Of course, sometimes we need to obtain relevant information about the class. We can use the following methods:
type(obj): to obtain the corresponding type of the object;
isinstance(obj, type ): Determine whether the object is an instance of the specified type;
hasattr(obj, attr): Determine whether the object has the specified attributes/methods;
getattr(obj, attr[, default] ) Get the value of the attribute/method. If there is no corresponding attribute, the default value is returned (provided that default is set), otherwise an AttributeError exception will be thrown;
setattr(obj, attr, value): Set the The value of the attribute/method, similar to obj.attr=value;
dir(obj): You can get a list of all attributes and method names of the corresponding object:
2. Method Access control
In fact, we can also regard methods as attributes of the class. Then the access control of methods is the same as attributes, and there is no actual private method. Everything relies on programmers to consciously abide by Python programming standards.
The example is as follows, the specific rules are the same as the attributes,
#!/usr/bin/env python # -*- coding: UTF-8 -*- class User(object): def upgrade(self): pass def _buy_equipment(self): pass def __pk(self): pass
3. Method decorator
@classmethod uses the class directly when calling Name class call, not an object
@property You can call the method just like accessing properties
See the example for specific usage:
#!/usr/bin/env python # -*- coding: UTF-8 -*- class UserInfo(object): lv = 5 def __init__(self, name, age, account): self.name = name self._age = age self.__account = account def get_account(self): return self.__account @classmethod def get_name(cls): return cls.lv @property def get_age(self): return self._age if __name__ == '__main__': userInfo = UserInfo('两点水', 23, 347073565); # 打印所有属性 print(dir(userInfo)) # 打印构造函数中的属性 print(userInfo.__dict__) # 直接使用类名类调用,而不是某个对象 print(UserInfo.lv) # 像访问属性一样调用方法(注意看get_age是没有括号的) print(userInfo.get_age)
The result of running:
Continuing Learning- Course Recommendations
- Courseware download
-
IntermediateFront-end Vue3 actual combat [handwritten vue project]
2857 people are watching -
ElementaryAPIPOST tutorial [Popularization of technical concepts related to network communication]
1795 people are watching -
IntermediateIssue 22_Comprehensive actual combat
5521 people are watching -
ElementaryIssue 22_PHP Programming
5172 people are watching -
ElementaryIssue 22_Front-end development
8713 people are watching -
IntermediateBig data (MySQL) video tutorial full version
4525 people are watching -
ElementaryGo language tutorial-full of practical information and no nonsense
2794 people are watching -
ElementaryGO Language Core Programming Course
2814 people are watching -
IntermediateJS advanced and BootStrap learning
2563 people are watching -
IntermediateSQL optimization and troubleshooting (MySQL version)
3374 people are watching -
IntermediateRedis+MySQL database interview tutorial
2963 people are watching -
ElementaryDeliver food or learn programming?
5708 people are watching
Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
Method | Description |
Constructor, call | |
Destructor, use | |
Print, convert | |
Assign value according to index | |
Get the value according to the index | |
Get the length | |
Comparison operation | |
Function call | |
Addition operation | |
Subtraction operation | |
Multiplication operation | ##__div__ |
__mod__ | |
__pow__ | |