Skip to content

Instantly share code, notes, and snippets.

class DogMeta(type):
def __new__(cls, name, bases, dct):
print(f">> DogMeta __new__: {name}")
print(f">> DogMeta __new__: {bases}")
print(f">> DogMeta __new__: {dct}")
x = super().__new__(cls, name, bases, dct)
x.race = "Dog"
return x
def bark(self):
print("Wooof! " + self.name)
def walk(self):
print("Hey! I'm walking!")
def init(self, name):
self.name = name
Dog = type("Dog", (object,), {"bark": bark, "walk": walk, "init": init})
@ustropo
ustropo / dog.py
Last active February 4, 2023 10:10
class Dog(object):
def init(self, name):
self.name = name
def bark(self):
print("Wooof! " + self.name)
def walk(self):
print("Hey! I'm walking!")
>>> x = 10
>>> a = 'some string'
>>> class V:
... pass
...
>>> type(x)
<class 'int'>
>>> type(a)
<class 'str'>
>>> type(V)
>>> class V:
... pass
...
>>> dir(V)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> V.__name__
>>> def add_one(x):
... return x + 1
...
>>> dir(add_one)
['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__',
'__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
'__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__',
'__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__']
>>> a = 10
>>> a.__class__
<class 'int'>
>>> a.real
10
>>> a.imag
0
>>> dir(a)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
class MyClass:
def __repr__(self) -> str:
return 'My Class Repr'
def __str__(self) -> str:
return 'My Super Class'
def __format__(self, __format_spec: str) -> str:
return 'My Formatted Class'
data = {'age': 10, 'name': 'John'}
arr = [0, 1, 2, 3, 4, 5]
print(f"The 1st element is {arr[0]}")
## 'The 1st element is 0'
print(f"Hi, {data['name']}! You'll be 18 in {18 - data['age']:02d} years")
## 'Hi, John! You'll be 18 in 08 years'
data = {'age': 10, 'name': 'John'}
arr = [0, 1, 2, 3, 4, 5]
print(f"The 1st element is {arr[0]}")
## 'The 1st element is 0'
print(f"Hi, {data['name']}! You'll be 18 in {18 - data['age']:02d} years")
## 'Hi, John! You'll be 18 in 08 years'