Python 中的 @staticmethod 和 @classmethod
2017-09-18 15:36
363 查看
视频中或者说书中,使用了
The reason to use staticmethod is if you have something that could be written as a standalone function (not part of any class), but you want to keep it within the class because it’s somehow semantically related to the class.
这个
这里为了使用方便,使用了
因为可以当成独立的函数,使用前不需要实例化:
Meaning of @classmethod and @staticmethod for beginner?
What is the difference between @staticmethod and @classmethod in Python?
Why do we use @staticmethod?
classmethod must have a reference to a class object as the first parameter, whereas staticmethod can have no parameters at all.
另外的解释:
@staticmethod function is nothing more than a function defined inside a class. It is callable without instantiating the class first. It’s definition is immutable via inheritance.
@classmethod function also callable without instantiating the class, but its definition follows Sub class, not Parent class, via inheritance. That’s because the first argument for @classmethod function must always be cls (class).
还有个解释:
@classmethod means: when this method is called, we pass the class as the first argument instead of the instance of that class (as we normally do with methods). This means you can use the class and its properties inside that method rather than a particular instance.
@staticmethod means: when this method is called, we don’t pass an instance of the class to it (as we normally do with methods). This means you can put a function inside a class but you can’t access the instance of that class (this is useful when your method does not use the instance).
@staticmethod,先把这个问题解决了。
class Config: ... @staticmethod def init_app(app): pass
The reason to use staticmethod is if you have something that could be written as a standalone function (not part of any class), but you want to keep it within the class because it’s somehow semantically related to the class.
这个
init_app函数和
Config类相关,但是本来不用写在
Config类中(没有传递
self参数),可以写成单独的函数。
这里为了使用方便,使用了
@staticmethod装饰器,将
init_app函数写在了
Config类中(可以使用
Config.init_app(app))。
因为可以当成独立的函数,使用前不需要实例化:
bootstrap = Bootstrap() mail = Mail() moment = Moment() db = SQLAlchemy() def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) config[config_name].init_app(app) # 直接使用了init_app(app)方法 bootstrap.init_app(app) #但是这儿init_app(app)方法为空。还是那个bootstrap对象。 mail.init_app(app) moment.init_app(app) db.init_app(app) from .main import main as main_blueprint app.register_blueprint(main_blueprint) return app
StackOverflow上几个相关的问题:
Meaning of @classmethod and @staticmethod for beginner?
What is the difference between @staticmethod and @classmethod in Python?
Why do we use @staticmethod?
classmethod must have a reference to a class object as the first parameter, whereas staticmethod can have no parameters at all.
另外的解释:
@staticmethod function is nothing more than a function defined inside a class. It is callable without instantiating the class first. It’s definition is immutable via inheritance.
@classmethod function also callable without instantiating the class, but its definition follows Sub class, not Parent class, via inheritance. That’s because the first argument for @classmethod function must always be cls (class).
还有个解释:
@classmethod means: when this method is called, we pass the class as the first argument instead of the instance of that class (as we normally do with methods). This means you can use the class and its properties inside that method rather than a particular instance.
@staticmethod means: when this method is called, we don’t pass an instance of the class to it (as we normally do with methods). This means you can put a function inside a class but you can’t access the instance of that class (this is useful when your method does not use the instance).
相关文章推荐
- python中@classmethod与@staticmethod的区别和用法
- python内部装饰器@classmethod, @staticmethod, @property
- 【再回首Python之美】【类-三种方法】绑定方法、@staticmethod、@classmethod的基本使用
- Python 实例方法、@staticmethod和@classmethod
- python @classmethod和@staticmethod的区别
- Python @classmethod 和 @staticmethod的比较
- Python 实例方法、@staticmethod和@classmethod
- Python中classmethod与staticmethod区别
- 飘逸的python - @staticmethod和@classmethod的作用与区别
- python @property,@staticmethod,@classmethod 使用例子
- python staticmethod和classmethod(转载)
- python staticmethod & classmethod
- Python中使用@staticmethod 和 @classmethod 装饰器
- python------@staticmethod和@classmethod的作用与区别
- Python @staticmethod 和 @classmethod 的异同
- Python 基础 —— @classmethod 与 @staticmethod 的意义及用途
- python中@staticmethod和@classmethod的作用与区别
- python的@classmethod和@staticmethod
- python的 @staticmethod,@classmethod和@property的区别
- python的静态方法以及@staticmethod或@classmethod的区别