blob: 006df8f81e339503263f801c30924f09eb283a60 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class Foo:
pass
def hello():
print('hello, world')
x = 13
class Square():
def __new__(cls, x):
instance = super(Square, cls).__new__(cls)
instance.__init__(x)
return instance.squared
def __init__(self, x):
self.inital = x
self.squared = x**2 # without .self the variable would be shared among all instances
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
area = Square(5.5) # if __new__ wasn't implemented, instead of 30.25 float print would call __str__ and return <class '__main__.Square'>: {'inital': 5.5, 'squared': 30.25}
print(area)
|