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)
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
Static method can be called without creating an object or instance. Simply create the method and call it directly.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor. However, they can contain a static constructor.
"A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the . NET Framework common language run-time (CLR) when the program or namespace containing the class is loaded."
Classes with only static methods is a common pattern in Java for utility methods. Examples in the standard library include Files, Collections, and Executors. For such utility classes, it's a good idea to make sure that your class cannot be instantiated, to make the intent of the class clear.
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)
You could use a classmethod
or staticmethod
class Paul(object): elems = [] @classmethod def addelem(cls, e): cls.elems.append(e) @staticmethod def addelem2(e): Paul.elems.append(e) Paul.addelem(1) Paul.addelem2(2) print(Paul.elems)
classmethod
has advantage that it would work with sub classes, if you really wanted that functionality.
module is certainly best though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With