Python version

update for python version >= 3.10: staticmethod functions can be called from within class scope just fine (for more info see: python issue tracker, or “what’s new”, or here) for python version <= 3.9 continue reading staticmethod objects apparently have a __func__ attribute storing the original raw function (makes sense that they had to). So … Read more

Python: Make class iterable

Add the __iter__ to the metaclass instead of the class itself (assuming Python 2.x): class Foo(object): bar = “bar” baz = 1 class __metaclass__(type): def __iter__(self): for attr in dir(self): if not attr.startswith(“__”): yield attr For Python 3.x, use class MetaFoo(type): def __iter__(self): for attr in dir(self): if not attr.startswith(“__”): yield attr class Foo(metaclass=MetaFoo): bar … Read more

Python decorator as a staticmethod

This is not how staticmethod is supposed to be used. staticmethod objects are descriptors that return the wrapped object, so they only work when accessed as classname.staticmethodname. Example class A(object): @staticmethod def f(): pass print A.f print A.__dict__[“f”] prints <function f at 0x8af45dc> <staticmethod object at 0x8aa6a94> Inside the scope of A, you would always … Read more

Why can overloaded operators not be defined as static members of a class?

Because there isn’t an obvious syntax to call such an operator, which would mean we’d have to make up something. Consider the following variables: X x1; X x2; Now, let’s pretend for a moment that we’re using normal member functions instead of operators – let’s say I changed operator+ to plus in your example. Each … Read more

How to create a custom EL function to invoke a static method?

First create a final class with a public static method which does exactly the job you want: package com.example; import java.util.Collection; public final class Functions { private Functions() { // Hide constructor. } public static boolean contains(Collection<Object> collection, Object item) { return collection.contains(item); } } Then define it as a facelet-taglib in /WEB-INF/functions.taglib.xml: <?xml version=”1.0″ … Read more

Why aren’t static methods considered good OO practice? [closed]

Object-orientation is about three things: messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. Of those three, the most important one is messaging. Static methods violate at least messaging and late-binding. The idea of messaging means that in OO, computation is performed by networks of self-contained objects which send … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)