创建没有实例的静态类 [英] Creating a static class with no instances

查看:60
本文介绍了创建没有实例的静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上看到的所有教程都展示了如何使用 __ init __ 构造函数方法创建类,以便可以声明该类型的对象或该类的实例。



如何创建一个类(在Java中为静态),以便可以访问该类的所有方法和属性,而无需创建新的实例/对象?



例如:

  class World:

allElems = []

def addElem(x):

allElems.append(x)

World.addElem(6)
print(World.allElems)






编辑



 类世界(对象):

allAirports = []

@staticmethod
def initialize():

f = open(os.path.expanduser(〜/ Desktop / 1000airports.csv))
file_reader = csv.reader( f)file_reader中col的



allAirports.append(Airport(col [0],col [ 2],col [3]))

错误:未定义名称 allAirports

解决方案

创建静态类的Python方式是简单地在类外部声明这些方法(Java将类用于对象和分组相关功能,但Python模块足以对不需要任何对象实例的相关功能进行分组。但是,如果您坚持在类级别上创建不需要实例的方法(而不是简单地使其成为模块中的独立函数),则可以使用 @staticmethod装饰器来实现。 / p>

也就是说,Python的方式是:

 #我的模块
元素= []

def add_element(x):
elements.append(x)

但是如果要镜像Java的结构,则可以执行以下操作:

  #我的模块
类World(object):
元素= []

@staticmethod
def add_element(x):
World.elements.append (x)

您也可以使用 @classmethod 如果您想知道特定的类(如果要允许从该类继承的类继承静态方法,这会很方便):

 #我的模块
类世界(对象):
个元素= []

@cl组装方法
def add_element(cls,x):
cls.elements.append(x)


All of the tutorials I see online show how to create classes with __init__ constructor methods so one can declare objects of that type, or instances of that class.

How do I create a class (static in Java) so that I can access all methods and attributes of that class without having to create new instances/objects?

For example:

class World:

    allElems = []

    def addElem(x):  

        allElems.append(x)

World.addElem(6)
print(World.allElems)


EDIT

class World(object):

    allAirports = []

    @staticmethod
    def initialize():

        f = open(os.path.expanduser("~/Desktop/1000airports.csv"))
        file_reader = csv.reader(f)

        for col in file_reader:

            allAirports.append(Airport(col[0],col[2],col[3]))

error: name 'allAirports' is not defined

解决方案

The Pythonic way to create a static class is simply to declare those methods outside of a class (Java uses classes both for objects and for grouping related functions, but Python modules are sufficient for grouping related functions that do not require any object instance). However, if you insist on making a method at the class level that doesn't require an instance (rather than simply making it a free-standing function in your module), you can do so by using the "@staticmethod" decorator.

That is, the Pythonic way would be:

# My module
elements = []

def add_element(x):
  elements.append(x)

But if you want to mirror the structure of Java, you can do:

# My module
class World(object):
  elements = []

  @staticmethod
  def add_element(x):
    World.elements.append(x)

You can also do this with @classmethod if you care to know the specific class (which can be handy if you want to allow the static method to be inherited by a class inheriting from this class):

# My module
class World(object):
  elements = []

  @classmethod
  def add_element(cls, x):
    cls.elements.append(x)

这篇关于创建没有实例的静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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