What is the difference between class method vs. class field function vs. class field arrow function?

There are differences between all 3 versions. This differences are in 3 areas: Who is this at runtime Where the function is assigned What is the type of this in typescript. Lets start with where they work just the same. Consider this class, with a class field: class Greeter { constructor(private x: string) { } … Read more

__getattr__ for static/class variables

__getattr__() and __str__() for an object are found on its class, so if you want to customize those things for a class, you need the class-of-a-class. A metaclass. class FooType(type): def _foo_func(cls): return ‘foo!’ def _bar_func(cls): return ‘bar!’ def __getattr__(cls, key): if key == ‘Foo’: return cls._foo_func() elif key == ‘Bar’: return cls._bar_func() raise AttributeError(key) … Read more

Attaching a decorator to all functions within a class

The cleanest way to do this, or to do other modifications to a class definition, is to define a metaclass. Alternatively, just apply your decorator at the end of the class definition using inspect: import inspect class Something: def foo(self): pass for name, fn in inspect.getmembers(Something, inspect.isfunction): setattr(Something, name, decorator(fn)) In practice of course you’ll … Read more

wait for Element Upgrade in connectedCallback: FireFox and Chromium differences

I think the Chrome/Safari behaviour is less intuitive for the beginners, but with some more complex scenarios (for example with child custom elements) then it is much more consistant. See the different examples below. They act strangely in Firefox… Another use case that I don’t have the courage to code: when a document is parsed, … Read more

Class methods which create new instances

They are class methods, not static methods1. This specific type, creating autoreleased objects, can be referred to as “factory methods” (formerly also “convenience constructors”), and they are discussed in the Concepts in ObjC Guide. They go something like this: + (instancetype)whatsisWithThingummy: (Thingummy *)theThingummy { return [[self alloc] initWithThingummy:theThingummy]; } Where Whatsis is your class, and … Read more

What is the purpose of class methods?

Class methods are for when you need to have methods that aren’t specific to any particular instance, but still involve the class in some way. The most interesting thing about them is that they can be overridden by subclasses, something that’s simply not possible in Java’s static methods or Python’s module-level functions. If you have … Read more

How to make a class property? [duplicate]

Here’s how I would do this: class ClassPropertyDescriptor(object): def __init__(self, fget, fset=None): self.fget = fget self.fset = fset def __get__(self, obj, klass=None): if klass is None: klass = type(obj) return self.fget.__get__(obj, klass)() def __set__(self, obj, value): if not self.fset: raise AttributeError(“can’t set attribute”) type_ = type(obj) return self.fset.__get__(obj, type_)(value) def setter(self, func): if not isinstance(func, … Read more