Bạn có thể xác định một chức năng bên ngoài một lớp và sau đó thêm nó. Tuy nhiên, có một sự khác biệt tinh tế trong việc gán hàm cho lớp hoặc cho đối tượng thể hiện. Đây là một ví dụ:
class MyClass1[object]:
def __init__[self, bar]:
self.foo = 'up'
MyClass1.foobar = bar
class MyClass2[object]:
def __init__[self, bar]:
self.foo = 'up'
self.foobar = bar
def bar[self]:
return "What's " + self.foo
Trước tiên hãy nhìn vào những gì đang xảy ra trong MyClass1
. foobar
Trong lớp này tương tự như một phương thức bình thường như thể nó được xác định bên trong định nghĩa lớp [nghĩa là nó là một phương thức bị ràng buộc với thể hiện của lớp này]. Hãy xem cái này trông như thế nào ...
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
Làm thế nào điều này khác với MyClass2
? Trong MyClass2
, foobar
chỉ đơn giản là một tham chiếu đến hàm thanh và không phải là phương thức ràng buộc. Vì điều này, chúng ta phải vượt qua thể hiện cho chức năng này hoạt động đúng. ví dụ.
In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
Mặc dù tôi không chắc đây có phải là thực hành tốt để làm theo cách này không ...
Inside_method3 inside_method
6
2 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
02In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
Các câu lệnh được đánh dấu là lỗi sẽ tạo ra lỗi khi thực thi vì biến không thể truy cập được ở đó.
& nbsp; biến được xác định bên trong phương thức:
Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class0
MyClass1
3In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
17Inside_method3 inside_method
6
2 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
02In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
Các câu lệnh được đánh dấu là lỗi sẽ tạo ra lỗi khi thực thi vì biến không thể truy cập được ở đó.
& nbsp; biến được xác định bên trong phương thức:
Các biến được xác định bên trong các phương thức chỉ có thể được truy cập trong phương thức đó chỉ bằng cách sử dụng tên biến. Ví dụ - var_name. Nếu bạn muốn sử dụng biến đó bên ngoài phương thức hoặc lớp, bạn phải khai báo biến đó là toàn cầu.
Outside_class1 outside_class
Outside_class2 outside_class
Outside_class3 outside_class
Outside_class4 outside_class
Outside_class5 outside_class
0MyClass1
3
2 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
17In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
8 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
28Inside_method3 inside_method6
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
42In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
5In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
6In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
7In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
8 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
9In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
5In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
3In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
7In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
6 In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
7In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
8In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
9Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class0
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
5Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class3
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
7Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class5
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class7
Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class8
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
8 Inside_class2 inside_class Inside_class3 inside_class0
In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
5Inside_class2 inside_class Inside_class3 inside_class4
Inside_class2 inside_class Inside_class3 inside_class5
In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0____26 Inside_class2 inside_class Inside_class3 inside_class8
In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
8In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
9Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class0
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
5Inside_method3 inside_method4
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
7Inside_method3 inside_method6
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 Inside_method3 inside_method8
Inside_method3 inside_method9
Output:
Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class
& nbsp; biến được xác định bên trong lớp:
Variable defined inside the class:
Các biến được xác định bên trong lớp nhưng bên ngoài phương thức có thể được truy cập trong lớp [tất cả các phương thức bao gồm] bằng cách sử dụng thể hiện của một lớp. Ví dụ: self.var_name. Nếu bạn muốn sử dụng biến đó ngay cả bên ngoài lớp, bạn phải khai báo biến đó là toàn cầu. Sau đó, biến có thể được truy cập bằng tên của nó bên trong và bên ngoài lớp và không sử dụng thể hiện của lớp.
If you want to use that variable even outside the class, you must declared that variable as a global. Then the variable can be accessed using its name inside and outside the class and not using the instance of the class.
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
8 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
9In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0MyClass1
3In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 MyClass1
5In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
5MyClass1
9foobar
0In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
6 In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
7In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
8In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
9Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class0
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
5foobar
9MyClass2
0In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
8MyClass2
2Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class5
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class7
Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class8
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
8 MyClass2
8In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4MyClass2
1In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0____26 Inside_class2 inside_class Inside_class3 inside_class8
In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
8In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
9Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class0
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4MyClass2
1Inside_method3 inside_method6
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
02Inside_method3 inside_method9
Output:
Inside_class2 inside_class Inside_class3 inside_class
Các câu lệnh được đánh dấu là lỗi sẽ tạo ra lỗi khi thực thi vì biến không thể truy cập được ở đó.
& nbsp; biến được xác định bên trong phương thức:
Variable defined inside the method:
Các biến được xác định bên trong các phương thức chỉ có thể được truy cập trong phương thức đó chỉ bằng cách sử dụng tên biến. Ví dụ - var_name. Nếu bạn muốn sử dụng biến đó bên ngoài phương thức hoặc lớp, bạn phải khai báo biến đó là toàn cầu.
If you want to use that variable outside the method or class, you have to declared that variable as a global.
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
8 In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
9In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4MyClass2
1In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
6 In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
7In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
8In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
9Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class5
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class7
Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class0
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
5In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
21foobar
0Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class5
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class7
Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class8
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
8 MyClass2
8In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4MyClass2
1In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
0In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
6 In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
7In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
8In [5]: y = MyClass2[bar]
In [6]: y.foobar
Out[6]:
In [7]: y.foobar[]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 y.foobar[]
TypeError: bar[] takes exactly 1 argument [0 given]
In [8]: y.foobar[y]
Out[8]: "What's up"
9Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class0
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
4MyClass2
1Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class5
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
2 Outside_class1 outside_class Outside_class2 outside_class Outside_class3 outside_class Outside_class4 outside_class Outside_class5 outside_class7
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
43Output:
Inside_method3 inside_method
In [2]: x = MyClass1[bar]
In [3]: x.foobar
Out[3]:
In [4]: x.foobar[]
Out[4]: "What's up"
8 MyClass2
8Summary: