如何在类及其方法中使用 self 参数、@staticmethod 关键字 [英] How to use self parameter, @staticmethod keyword inside a class and its methods

查看:58
本文介绍了如何在类及其方法中使用 self 参数、@staticmethod 关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 类,它有多种方法.我已经通过 @staticmethod 实例定义了我的方法,我想从我的主函数(main_function)内部调用我的类的其他方法.我想我需要 self 参数来从我的主函数调用我的其他函数,我想在创建我的类的实例时将此参数传递给我的 main_function.

I have a python class which has multiple methods. I have defined my methods via @staticmethod instance and I want to call other methods of my class from inside my main function(main_function). I think I need self parameter for calling my other functions from my main function and I want to pass this parameter to my main_function when I create an instance of my class.

class myclass:
  @staticmethod
  def function1(param1)
      print "function1"
  @staticmethod
  def main_function(self, param1)
     function1(param1)

my_object = myclass()
my_object.main_function(param1)

我收到此错误:

TypeError: main_function() takes exactly 2 arguments (1 given)

问题是我在创建实例时没有 self 参数.我试图从我的方法定义中删除 @staticmethod 关键字并删除所有使用的 self 参数,但这不起作用.

The problem is that I have not self parameter when I create my instance. I tried to remove @staticmethod keyword from my method definition and remove all self parameter using, but this does not work.

推荐答案

仅当您正在创建一个通常希望绑定到特定类但做的函数时,才使用 @staticmethod不需要任何其他上下文.例如,str.maketrans() 函数 是一个静态方法,因为它是一个在处理字符串时经常使用的实用函数,将它命名为已经存在的 str 类型(它作为一个类预先存在)使得感觉在那里.

Only use @staticmethod if you are creating a function that you'd normally want to tie to specific classes but do not need any other context. For example, the str.maketrans() function is a static method because it is a utility function you'd often use when working with strings, namespacing it to the already-existing str type (which pre-exists as a class) makes sense there.

您似乎将类用作命名空间.不要那样做.为您的函数使用模块,您不必担心适用于类的特殊作用域规则.仅在需要将状态功能捆绑在一起时才使用类.

You appear to be using classes as a namespace instead. Don't do that. Use a module for your functions, and you don't have to worry about the special scoping rules that apply to classes. Only use a class when you need to bundle state with functionality.

如果你坚持使用带有静态方法的类无论如何,你就会在任何地方硬编码类名:

If you insist on using classes with static methods anyway, you are stuck with hardcoding the class name everywhere:

class myclass:
    @staticmethod
    def function1(param1)
        print "function1"

    @staticmethod
    def main_function(param1)
        # Want to use other functions in this class? Then you will
        # have to use the full name of the class as a prefix:
        myclass.function1(param1)

你可以使用 classmethods 代替,这样你就可以引用类对象:

You could make use of classmethods instead so you have a reference to the class object:

class myclass:
    @staticmethod
    def function1(param1)
        print "function1"

    @classmethod
    def main_function(cls, param1)
        # Now you can use the `cls` reference to access other attributes
        cls.function1(param1)

这有一个额外的好处,你可以使用继承.

This has the added advantage that you can use inheritance.

然而,使用模块是将一组函数组织到命名空间中的正确方法.将所有内容放入包中的 my_module.py 文件中,并使用导入;

However, using a module is the correct way to organise a set of functions into a namespace. Put everything into a my_module.py file in your package, and use importing;

import my_module

my_module.main_function(param1)

现在 my_module 中的所有全局变量都捆绑到一个模块对象中,不需要前缀或 cls 引用.

Now all globals in my_module are bundled into one module object, and no prefixing or cls references are needed.

这篇关于如何在类及其方法中使用 self 参数、@staticmethod 关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆