Hướng dẫn python class pass argument - đối số vượt qua lớp python

Tôi chỉ viết lại một chương trình làm việc thành các chức năng trong một lớp học và mọi thứ đã gây rối.

Nội dung chính ShowShow

  • Python - Các lớp và trường hợp (__init__, __call__, v.v.)
  • Làm thế nào để bạn chuyển một cuộc tranh luận cho một lớp học trong Python?
  • Lớp Python có thể lấy lập luận không?
  • Làm thế nào để bạn chuyển một biến cho một lớp khác trong Python?
  • Bạn có thể chuyển một lớp cho một python chức năng không?

Đầu tiên, trong phần

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
7 của lớp tôi đã khai báo một loạt các biến với
class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
8., in the 7 của lớp tôi đã khai báo một loạt các biến với
class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
8.
, in the

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
7 section of the class I declared a bunch of variables with
class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
8.

Tôi có thể truy cập/sửa đổi các biến này trong mọi chức năng của lớp bằng cách sử dụng

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
9 trong hàm đó không? Nói cách khác, bằng cách tuyên bố
class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
9 tôi đã thực hiện các biến này, các biến toàn cầu trong phạm vi của lớp phải không?

Nếu không, làm thế nào để tôi tự xử lý bản thân?

Thứ hai, làm cách nào để chuyển chính xác các đối số cho lớp?, how do I correctly pass arguments to the class?, how do I correctly pass arguments to the class?

Thứ ba, làm thế nào để tôi gọi một chức năng của lớp bên ngoài phạm vi lớp?, how do I call a function of the class outside of the class scope?, how do I call a function of the class outside of the class scope?

Fouth, làm cách nào để tạo một thể hiện của

s = Student(args)
1 trong một
s = Student(args)
2 khác, chuyển các biến từ
s = Student(args)
3 đến
s = Student(args)
4?, how do I create an Instance of the 1 trong một
s = Student(args)
2 khác, chuyển các biến từ
s = Student(args)
3 đến
s = Student(args)
4?
, how do I create an Instance of the

s = Student(args)
1 in another
s = Student(args)
2, passing variables from
s = Student(args)
3 to
s = Student(args)
4?

Tôi muốn gọi một hàm từ

s = Student(args)
3 với các đối số từ
s = Student(args)
4. Những gì tôi đã làm cho đến nay là.
class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
6

Tôi rõ ràng không hiểu cách truyền các biến cho các lớp hoặc cách xử lý

s = Student(args)
7, khi nào nên sử dụng nó và khi nào không. Tôi có lẽ cũng không hiểu làm thế nào để tạo đúng một thể hiện của một lớp. Nói chung, tôi không hiểu cơ học của các lớp học vì vậy xin hãy giúp tôi và giải thích cho tôi như tôi không biết (điều mà tôi không, dường như). Hoặc chỉ cho tôi một video kỹ lưỡng, hoặc hướng dẫn có thể đọc được.

Tất cả những gì tôi tìm thấy trên web là những ví dụ siêu đơn giản, điều đó không giúp tôi nhiều. Hoặc chỉ các định nghĩa rất ngắn về các lớp và phương thức lớp, v.v.

Tôi có thể gửi cho bạn mã gốc của tôi nếu các bạn muốn, nhưng nó khá dài.

Python - Các lớp và trường hợp (__init__, __call__, v.v.)

Hướng dẫn python class pass argument - đối số vượt qua lớp python



Tìm kiếm trang web Bogotobogo.com:


Các lớp học và trường hợp

Không giống như C ++, các lớp trong Python là các đối tượng theo cách riêng của chúng, ngay cả khi không có trường hợp. Họ chỉ là những không gian tên khép kín. Do đó, miễn là chúng tôi có một tham chiếu đến một lớp, chúng tôi có thể đặt hoặc thay đổi các thuộc tính của nó bất cứ lúc nào chúng tôi muốn.classes in Python are objects in their own right, even without instances. They are just self-contained namespaces. Therefore, as long as we have a reference to a class, we can set or change its attributes anytime we want. classes in Python are objects in their own right, even without instances. They are just self-contained namespaces. Therefore, as long as we have a reference to a class, we can set or change its attributes anytime we want.

Xác định các lớp học

Câu lệnh sau đây tạo ra một lớp không có thuộc tính được đính kèm và trên thực tế, đó là một đối tượng không gian tên trống:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
8

Tên của lớp này là học sinh và nó không được thừa hưởng từ bất kỳ lớp nào khác. Tên lớp thường được viết hoa, nhưng đây chỉ là một quy ước, không phải là một yêu cầu. Tất cả mọi thứ trong một lớp đều được thụt vào, giống như mã trong một hàm, nếu câu lệnh, cho vòng lặp hoặc bất kỳ khối mã nào khác. Dòng đầu tiên không thụt vào bên ngoài lớp.Student, and it doesn't inherit from any other class. Class names are usually capitalized, but this is only a convention, not a requirement. Everything in a class is indented, just like the code within a function, if statement, for loop, or any other block of code. The first line not indented is outside the class.Student, and it doesn't inherit from any other class. Class names are usually capitalized, but this is only a convention, not a requirement. Everything in a class is indented, just like the code within a function, if statement, for loop, or any other block of code. The first line not indented is outside the class.

Trong mã, thẻ vượt qua là câu lệnh không hoạt động. Lớp học sinh này không định nghĩa bất kỳ phương thức hoặc thuộc tính nào, nhưng về mặt cú pháp, cần phải có một cái gì đó trong định nghĩa, do đó, câu lệnh PASS. Đây là một từ dành riêng Python chỉ có nghĩa là di chuyển, không có gì để xem ở đây. Đó là một tuyên bố không làm gì cả, và đó là một trình giữ chỗ tốt khi chúng ta bỏ ra các chức năng hoặc lớp học. Câu lệnh PASS trong Python giống như một tập hợp trống của niềng răng xoăn {} trong java hoặc C.pass is the no-operation statement. This Student class doesn't define any methods or attributes, but syntactically, there needs to be something in the definition, thus the pass statement. This is a Python reserved word that just means move along, nothing to see here. It's a statement that does nothing, and it's a good placeholder when we're stubbing out functions or classes. The pass statement in Python is like an empty set of curly braces {} in Java or C.pass is the no-operation statement. This Student class doesn't define any methods or attributes, but syntactically, there needs to be something in the definition, thus the pass statement. This is a Python reserved word that just means move along, nothing to see here. It's a statement that does nothing, and it's a good placeholder when we're stubbing out functions or classes. The pass statement in Python is like an empty set of curly braces {} in Java or C.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
9

Sau đó, chúng tôi đính kèm các thuộc tính vào lớp bằng cách gán tên cho nó bên ngoài lớp. Trong trường hợp này, lớp học về cơ bản là một OBJEC với các tên trường được gắn vào nó.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
0

Lưu ý rằng điều này đang hoạt động mặc dù chưa có trường hợp nào của lớp.

Phương pháp __init __ ()

Nhiều lớp được kế thừa từ các lớp khác, nhưng một lớp trong ví dụ là không. Nhiều lớp xác định các phương thức, nhưng cái này thì không. Không có gì mà một lớp Python hoàn toàn phải có, ngoài một cái tên. Cụ thể, các lập trình viên C ++ có thể thấy kỳ lạ là các lớp Python không có các hàm tạo và phá hủy rõ ràng. Mặc dù không bắt buộc, các lớp Python có thể có một cái gì đó tương tự như hàm tạo: phương thức __init __ ().__init__() method.__init__() method.

Trong Python, các đối tượng được tạo thành hai bước:

  1. Xây dựng một đối tượng __new () ____new()__
    __new()__
  2. Khởi tạo đối tượng __init () ____init()__
    __init()__

Tuy nhiên, rất hiếm khi thực sự cần phải thực hiện __new () __ vì Python xây dựng các đối tượng của chúng tôi cho chúng tôi. Vì vậy, trong hầu hết các trường hợp, chúng tôi thường chỉ thực hiện phương pháp đặc biệt, __init () __.__new()__ because Python constructs our objects for us. So, in most of the cases, we usually only implement the special method, __init()__. __new()__ because Python constructs our objects for us. So, in most of the cases, we usually only implement the special method, __init()__.

Hãy tạo một lớp lưu trữ một chuỗi và một số:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id

Khi một def xuất hiện bên trong một lớp, nó thường được gọi là phương pháp. Nó tự động nhận được một đối số đầu tiên đặc biệt, self, cung cấp một tay cầm trở lại thể hiện sẽ được xử lý. Các phương pháp với hai dấu gạch dưới ở đầu và cuối tên là các phương pháp đặc biệt.def appears inside a class, it is usually known as a method. It automatically receives a special first argument, self, that provides a handle back to the instance to be processed. Methods with two underscores at the start and end of names are special methods.def appears inside a class, it is usually known as a method. It automatically receives a special first argument, self, that provides a handle back to the instance to be processed. Methods with two underscores at the start and end of names are special methods.

Phương thức __init __ () được gọi ngay sau khi một thể hiện của lớp được tạo. Sẽ rất hấp dẫn khi gọi đây là hàm tạo của lớp. Nó thực sự hấp dẫn, bởi vì nó trông giống như một hàm tạo C ++ và theo quy ước, phương thức __init __ () là phương thức đầu tiên được xác định cho lớp. Nó dường như hoạt động giống như một nhà xây dựng bởi vì đây là đoạn mã đầu tiên được thực thi trong một thể hiện mới được tạo của lớp. Tuy nhiên, nó không giống như một hàm tạo, bởi vì đối tượng đã được xây dựng vào thời điểm phương thức __init () __ được gọi và chúng tôi đã có một tham chiếu hợp lệ về thể hiện mới của lớp.__init__() method is called immediately after an instance of the class is created. It would be tempting to call this the constructor of the class. It's really tempting, because it looks like a C++ constructor, and by convention, the __init__() method is the first method defined for the class. It appears to be acting like a constructor because it's the first piece of code executed in a newly created instance of the class. However, it's not like a constructor, because the object has already been constructed by the time the __init()__ method is called, and we already have a valid reference to the new instance of the class.__init__() method is called immediately after an instance of the class is created. It would be tempting to call this the constructor of the class. It's really tempting, because it looks like a C++ constructor, and by convention, the __init__() method is the first method defined for the class. It appears to be acting like a constructor because it's the first piece of code executed in a newly created instance of the class. However, it's not like a constructor, because the object has already been constructed by the time the __init()__ method is called, and we already have a valid reference to the new instance of the class.

Tham số đầu tiên của phương thức __init () __, tự, tương đương với cấu hình của C ++. Mặc dù chúng tôi không phải vượt qua nó vì Python sẽ làm điều đó cho chúng tôi, chúng tôi phải đặt bản thân làm tham số đầu tiên của các phương thức không phải là phương pháp. Nhưng bản thân luôn luôn rõ ràng trong Python để làm cho quyền truy cập thuộc tính rõ ràng hơn.__init()__ method, self, is equivalent to this of C++. Though we do not have to pass it since Python will do it for us, we must put self as the first parameter of nonstatic methods. But the self is always explicit in Python to make attribute access more obvious.__init()__ method, self, is equivalent to this of C++. Though we do not have to pass it since Python will do it for us, we must put self as the first parameter of nonstatic methods. But the self is always explicit in Python to make attribute access more obvious.

Bản thân luôn là một tham chiếu đến ví dụ hiện tại của lớp. Mặc dù lập luận này đảm nhận vai trò của từ dành riêng trong C ++ hoặc Java, nhưng bản thân không phải là một từ dành riêng trong Python, chỉ đơn thuần là một quy ước đặt tên. Tuy nhiên, xin đừng gọi nó là bất cứ điều gì ngoài bản thân; Đây là một quy ước rất mạnh mẽ.self is always a reference to the current instance of the class. Though this argument fills the role of the reserved word this in c++ or Java, but self is not a reserved word in Python, merely a naming convention. Nonetheless, please don't call it anything but self; this is a very strong convention.self is always a reference to the current instance of the class. Though this argument fills the role of the reserved word this in c++ or Java, but self is not a reserved word in Python, merely a naming convention. Nonetheless, please don't call it anything but self; this is a very strong convention.

Khi một phương thức gán cho một thuộc tính tự, nó sẽ tạo một thuộc tính trong một thể hiện vì tự đề cập đến thể hiện được xử lý.self attribute, it creates an attribute in an instance because self refers to the instance being processed.self attribute, it creates an attribute in an instance because self refers to the instance being processed.

Các lớp học khởi tạo

Để khởi tạo một lớp, chỉ cần gọi lớp như thể đó là một hàm, chuyển các đối số mà phương thức __init __ () yêu cầu. Giá trị trả về sẽ là đối tượng mới được tạo. Trong Python, không có nhà điều hành mới rõ ràng như có trong C ++ hoặc Java. Vì vậy, chúng tôi chỉ cần gọi một lớp như thể đó là một hàm để tạo một thể hiện mới của lớp:__init__() method requires. The return value will be the newly created object. In Python, there is no explicit new operator like there is in c++ or Java. So, we simply call a class as if it were a function to create a new instance of the class:__init__() method requires. The return value will be the newly created object. In Python, there is no explicit new operator like there is in c++ or Java. So, we simply call a class as if it were a function to create a new instance of the class:

s = Student(args)

Chúng tôi đang tạo một thể hiện của lớp sinh viên và gán thể hiện mới được tạo cho biến s. Chúng tôi đang chuyển một tham số, ARGS, sẽ kết thúc như là đối số trong phương thức __init __ () của sinh viên.Student class and assigning the newly created instance to the variable s. We are passing one parameter, args, which will end up as the argument in Student's __init__() method.Student class and assigning the newly created instance to the variable s. We are passing one parameter, args, which will end up as the argument in Student's __init__() method.

S bây giờ là một ví dụ của lớp học sinh. Mỗi phiên bản lớp có một thuộc tính tích hợp, __ class__, đó là lớp của đối tượng. Các lập trình viên Java có thể quen thuộc với lớp lớp, trong đó chứa các phương thức như getName () và getSuperClass () để lấy thông tin siêu dữ liệu về một đối tượng. Trong Python, loại siêu dữ liệu này có sẵn thông qua các thuộc tính, nhưng ý tưởng là như nhau. is now an instance of the Student class. Every class instance has a built-in attribute, __class__, which is the object's class. Java programmers may be familiar with the Class class, which contains methods like getName() and getSuperclass() to get metadata information about an object. In Python, this kind of metadata is available through attributes, but the idea is the same. is now an instance of the Student class. Every class instance has a built-in attribute, __class__, which is the object's class. Java programmers may be familiar with the Class class, which contains methods like getName() and getSuperclass() to get metadata information about an object. In Python, this kind of metadata is available through attributes, but the idea is the same.

Chúng ta có thể truy cập tài liệu của phiên bản giống như với một hàm hoặc một mô -đun. Tất cả các trường hợp của một lớp chia sẻ cùng một tài liệu.docstring just as with a function or a module. All instances of a class share the same docstring.docstring just as with a function or a module. All instances of a class share the same docstring.

Chúng ta có thể sử dụng lớp học sinh được xác định ở trên như sau:Student class defined above as following:Student class defined above as following:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
3

Không giống như C ++, các thuộc tính của đối tượng Python là công khai, chúng ta có thể truy cập chúng bằng toán tử DOT (.):

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
4

Chúng ta cũng có thể gán một giá trị mới cho thuộc tính:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
5

Làm thế nào về sự phá hủy đối tượng? Python có bộ sưu tập rác tự động. Trên thực tế, khi một đối tượng sắp được thu thập rác, phương thức __del () __ của nó được gọi, với bản thân là đối số duy nhất của nó. Nhưng chúng tôi hiếm khi sử dụng phương pháp này. Python has automatic garbage collection. Actually, when an object is about to be garbage-collected, its __del()__ method is called, with self as its only argument. But we rarely use this method.
Python has automatic garbage collection. Actually, when an object is about to be garbage-collected, its __del()__ method is called, with self as its only argument. But we rarely use this method.

Ví dụ

Hãy xem xét một ví dụ khác:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
6

Bản thân là gì? Đó là một biến thể hiện. Nó hoàn toàn tách biệt với ID, được chuyển vào phương thức __init __ () như một đối số. self.id là toàn cầu cho trường hợp. Điều đó có nghĩa là chúng ta có thể truy cập nó từ các phương pháp khác. Các biến thể hiện là cụ thể cho một trường hợp của một lớp. Ví dụ: nếu chúng ta tạo hai phiên bản sinh viên với các giá trị ID khác nhau, mỗi người sẽ nhớ các giá trị riêng của họ.self.id? It's an instance variable. It is completely separate from id, which was passed into the __init__() method as an argument. self.id is global to the instance. That means that we can access it from other methods. Instance variables are specific to one instance of a class. For example, if we create two Student instances with different id values, they will each remember their own values.self.id?
It's an instance variable. It is completely separate from id, which was passed into the __init__() method as an argument. self.id is global to the instance. That means that we can access it from other methods. Instance variables are specific to one instance of a class. For example, if we create two Student instances with different id values, they will each remember their own values.

Sau đó, hãy thực hiện hai trường hợp:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
80

Ở đây, chúng tôi tạo ra các đối tượng thể hiện. Các đối tượng này chỉ là các không gian tên có quyền truy cập vào các thuộc tính của các lớp của chúng. Hai trường hợp có liên kết trở lại lớp mà chúng được tạo. Nếu chúng ta sử dụng một thể hiện với tên của một thuộc tính của đối tượng lớp, Python lấy tên từ lớp.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
81

Lưu ý rằng cả S1 và S2 đều không có thuộc tính setData () của riêng mình. Vì vậy, Python theo liên kết từ ví dụ này đến lớp để tìm thuộc tính.s1 nor s2 has a setData() attribute of its own. So, Python follows the link from instance to class to find the attribute. s1 nor s2 has a setData() attribute of its own. So, Python follows the link from instance to class to find the attribute.

Trong hàm setData () bên trong sinh viên, giá trị được chuyển vào được gán cho self.data. Trong một phương thức, tự động đề cập đến thể hiện được xử lý (S1 hoặc S2). Do đó, các giá trị lưu trữ gán trong các không gian tên của các trường hợp, không phải của lớp.setData() function inside Student, the value passed in is assigned to self.data. Within a method, self automatically refers to the instance being processed (s1 or s2). Thus, the assignment store values in the instances' namespaces, not the class's.setData() function inside Student, the value passed in is assigned to self.data. Within a method, self automatically refers to the instance being processed (s1 or s2). Thus, the assignment store values in the instances' namespaces, not the class's.

Khi chúng tôi gọi phương thức hiển thị của lớp () để in self.data, chúng tôi thấy self.data khác nhau trong mỗi trường hợp. Nhưng bản thân Hiển thị tên () giống nhau trong S1 và S2:display() method to print self.data, we see the self.data differs in each instance. But the name display() itself is the same in s1 and s2: display() method to print self.data, we see the self.data differs in each instance. But the name display() itself is the same in s1 and s2:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
82

Lưu ý rằng chúng tôi đã lưu trữ các loại đối tượng khác nhau trong thành viên dữ liệu trong mỗi trường hợp. Trong Python, không có tuyên bố nào cho các thuộc tính (thành viên). Chúng ra đời khi chúng được gán giá trị. Thuộc tính có tên dữ liệu thậm chí không tồn tại trong bộ nhớ cho đến khi nó được gán trong phương thức setData ().data member in each instance. In Python, there are no declarations for instance attributes (members). They come into existence when they are assigned values. The attribute named data does not even exist in memory until it is assigned within the setData() method. data member in each instance. In Python, there are no declarations for instance attributes (members). They come into existence when they are assigned values. The attribute named data does not even exist in memory until it is assigned within the setData() method.

Ví dụ a

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
83

Sau đó, chúng tôi tạo các đối tượng thể hiện:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
84

Các đối tượng thể hiện chỉ là các không gian tên có quyền truy cập vào các thuộc tính của các lớp của chúng. Trên thực tế, tại thời điểm này, chúng ta có ba đối tượng: một lớp và hai trường hợp. Lưu ý rằng cả A và A2 đều không có thuộc tính SetData của riêng mình. Tuy nhiên, giá trị được chuyển vào setData được gán cho self.data. Trong một phương thức, tự động đề cập đến thể hiện được xử lý (A hoặc A2). Vì vậy, các bài tập lưu trữ các giá trị trong các không gian tên của các trường hợp, không phải của lớp. Các phương thức phải thông qua đối số tự để có được thể hiện được xử lý. Chúng ta có thể thấy nó từ đầu ra:a nor a2 has a setData attribute of its own. However, the value passed into the setData is assigned to self.data. Within a method, self automatically refers to the instance being processed (a or a2). So, the assignments store values in the instances' namespaces, not the class's. Methods must go through the self argument to get the instance to be processed. We can see it from the output:a nor a2 has a setData attribute of its own. However, the value passed into the setData is assigned to self.data. Within a method, self automatically refers to the instance being processed (a or a2). So, the assignments store values in the instances' namespaces, not the class's. Methods must go through the self argument to get the instance to be processed. We can see it from the output:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
85

Như chúng tôi dự kiến, chúng tôi đã lưu trữ giá trị cho từng đối tượng thể hiện mặc dù chúng tôi đã sử dụng cùng một phương thức, hiển thị. Bản thân đã tạo ra tất cả những khác biệt! Nó đề cập đến các trường hợp.display. The self made all the differences! It refers to instances.display. The self made all the differences! It refers to instances.

Ví dụ b

Siêu lớp được liệt kê trong ngoặc đơn trong tiêu đề lớp như chúng ta thấy ví dụ dưới đây.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
86

MyClassB xác định lại màn hình của siêu lớp của nó và nó thay thế thuộc tính hiển thị trong khi vẫn kế thừa phương thức SetData trong MyClassa như chúng ta thấy dưới đây: redefines the display of its superclass, and it replaces the display attribute while still inherits the setData method in MyClassA as we see below: redefines the display of its superclass, and it replaces the display attribute while still inherits the setData method in MyClassA as we see below:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
87

Nhưng đối với ví dụ của MyClassa vẫn đang sử dụng màn hình, trước đây được xác định trong MyClassa.MyClassA is still using the display, previously defined in MyClassA.MyClassA is still using the display, previously defined in MyClassA.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
88 Ví dụ c

example C

Quá tải toán tử cho phép các đối tượng chặn và phản hồi các hoạt động. Nó làm cho các giao diện đối tượng nhất quán hơn và nó cũng cho phép các đối tượng lớp hoạt động như tích hợp

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
89

MyClassc là một myClassb và các phiên bản của nó kế thừa phương thức Display () từ myClassb. is a MyClassB, and its instances inherits the display() method from MyClassB. is a MyClassB, and its instances inherits the display() method from MyClassB.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
90

Khi MyClassc được tạo, một đối số '123' được thông qua. Điều này được chuyển cho đối số d trong hàm tạo __init__ và được gán cho self.data. Trên thực tế, MyClassc sắp xếp để đặt thuộc tính dữ liệu tự động tại thời điểm xây dựng. Nó không yêu cầu gọi setData () từ trường hợp sau đó.MyClassC is created, an argument '123' is passed. This is passed to the d argument in the __init__ constructor and assigned to self.data. Actually, MyClassC arranges to set the data attribute automatically at construction time. It does not require calling setdata() from instance at later time. MyClassC is created, an argument '123' is passed. This is passed to the d argument in the __init__ constructor and assigned to self.data. Actually, MyClassC arranges to set the data attribute automatically at construction time. It does not require calling setdata() from instance at later time.

Đối với +, Python chuyển đối tượng thể hiện ở bên trái của đối số tự trong __add__ và giá trị bên phải đối với D2. Để in (), Python chuyển đối tượng được in thành bản thân trong __str__. Som bất kỳ chuỗi nào Phương thức này trả về được coi là chuỗi in cho đối tượng. Bằng cách triển khai __str__, chúng ta có thể sử dụng in để hiển thị các đối tượng của lớp này và chúng ta không phải gọi phương thức Display ().+, Python passes the instance object on the left of the self argument in __add__ and the value on the right to d2. For print(), Python passes the object being printed to self in __str__. Som whatever string this method returns is taken to be the print string for the object. By implementing __str__, we can use print to display objects of this class, and we do not have to call the display() method.+, Python passes the instance object on the left of the self argument in __add__ and the value on the right to d2. For print(), Python passes the object being printed to self in __str__. Som whatever string this method returns is taken to be the print string for the object. By implementing __str__, we can use print to display objects of this class, and we do not have to call the display() method.

__Add__ tạo và trả về một đối tượng phiên bản mới bằng cách gọi myClassc. Tuy nhiên, MUL thay đổi đối tượng hiện tại tại chỗ bằng cách chỉ định lại thuộc tính tự.__add__ makes and returns a new instance object by calling MyClassC. However, mul changes the current instance object in-place by reassigning the self attribute. __add__ makes and returns a new instance object by calling MyClassC. However, mul changes the current instance object in-place by reassigning the self attribute.

Phương pháp

Dưới đây là một ví dụ về hình chữ nhật lớp với chức năng thành viên trả về khu vực của nó.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
91

Lưu ý rằng phiên bản này đang sử dụng quyền truy cập thuộc tính trực tiếp cho chiều rộng và chiều cao.

Chúng tôi có thể đã sử dụng các phương thức Setter và Getter triển khai sau đây:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
92

Các thuộc tính đối tượng là nơi chúng tôi lưu trữ thông tin của chúng tôi và hầu hết các trường hợp cú pháp sau là đủ:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
93

Tuy nhiên, có những trường hợp khi cần có sự linh hoạt hơn. Ví dụ: để xác thực các phương thức setter và getter, chúng ta có thể cần thay đổi toàn bộ mã như sau:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
94 Thuộc tính

Properties

Giải pháp cho vấn đề linh hoạt là cho phép chúng tôi tự động chạy mã trên quyền truy cập thuộc tính, nếu cần. Các thuộc tính cho phép chúng tôi định tuyến một quyền truy cập thuộc tính cụ thể (thuộc tính GET và đặt các hoạt động) thành các chức năng hoặc phương thức chúng tôi cung cấp, cho phép chúng tôi chèn mã được tự động chạy. Một thuộc tính được tạo bằng cách gán kết quả của hàm tích hợp cho thuộc tính lớp:properties allow us to route a specific attribute access (attribute's get and set operations) to functions or methods we provide, enabling us to insert code to be run automatically. A property is created by assigning the result of a built-in function to a class attribute: properties allow us to route a specific attribute access (attribute's get and set operations) to functions or methods we provide, enabling us to insert code to be run automatically. A property is created by assigning the result of a built-in function to a class attribute:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
95 Chúng tôi vượt qua We pass
    We pass
  1. FGET: Một hàm để chặn thuộc tính tìm nạp: a function for intercepting attribute fetches: a function for intercepting attribute fetches
  2. FSET: một chức năng cho các bài tập: a function for assignments: a function for assignments
  3. FDEL: Một hàm để xóa thuộc tính: a function for attribute deletions: a function for attribute deletions
  4. Doc: Nhận chuỗi tài liệu cho thuộc tính: receives a documentation string for the attribute: receives a documentation string for the attribute

Nếu chúng ta quay lại mã trước đó và thêm thuộc tính (), thì mã trông như thế này:property(), then the code looks like this:property(), then the code looks like this:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
96

Chúng ta có thể sử dụng lớp như dưới đây:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
97

Ví dụ trên chỉ đơn giản là truy cập truy cập thuộc tính. Tuy nhiên, các thuộc tính thường tính toán giá trị của một thuộc tính động khi tìm nạp, như ví dụ sau minh họa:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
98

Lớp xác định một thuộc tính v được truy cập như thể đó là dữ liệu tĩnh. Tuy nhiên, nó thực sự chạy mã để tính toán giá trị của nó khi được tìm nạp. Khi mã chạy, giá trị được lưu trữ trong trường hợp dưới dạng thông tin trạng thái, nhưng bất cứ khi nào chúng tôi truy xuất nó thông qua thuộc tính được quản lý, giá trị của nó sẽ tự động bình phương.V that is accessed as though it were static data. However, it really runs code to compute its value when fetched. When the code runs, the value is stored in the instance as state information, but whenever we retrieve it via the managed attribute, its value is automatically squared.V that is accessed as though it were static data. However, it really runs code to compute its value when fetched. When the code runs, the value is stored in the instance as state information, but whenever we retrieve it via the managed attribute, its value is automatically squared.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
99

Một lần nữa, lưu ý rằng Fetch tính toán hình vuông của dữ liệu của phiên bản.

Quá tải người vận hành: 2.6 __cmp __ () (Đã xóa trong 3.0)

Bằng cách thực hiện phương thức __CMP __ (), tất cả các toán tử so sánh (, v.v.) sẽ hoạt động.__cmp__() method, all of the comparison operators(, etc.) will work.__cmp__() method, all of the comparison operators(<, ==, !=, >, etc.) will work.

Vì vậy, hãy thêm phần sau vào lớp hình chữ nhật của chúng tôi:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
00

Lưu ý rằng chúng tôi đã sử dụng hàm cmp () tích hợp để triển khai __cmp__. Hàm cmp () trả về -1 nếu đối số đầu tiên nhỏ hơn thứ hai, 0 nếu chúng bằng nhau và 1 nếu đối số thứ nhất lớn hơn thứ hai.cmp() function to implement __cmp__. The cmp() function returns -1 if the first argument is less than the second, 0 if they are equal, and 1 if the first argument is greater than the second.cmp() function to implement __cmp__. The cmp() function returns -1 if the first argument is less than the second, 0 if they are equal, and 1 if the first argument is greater than the second.

Đối với Python 3.0, chúng tôi nhận được kiểu loại: các loại không thể đặt hàng. Vì vậy, chúng ta cần sử dụng các phương thức cụ thể vì các hàm tích hợp __cmp __ () và cmp () được loại bỏ trong Python 3.0.__cmp__() and cmp() built-in functions are removed in Python 3.0.__cmp__() and cmp() built-in functions are removed in Python 3.0.

Quá tải người vận hành: __str__

__Str__ là toán tử được sử dụng phổ biến thứ 2 quá tải trong Python sau __init__. __Str__ được chạy tự động bất cứ khi nào một thể hiện được chuyển đổi thành chuỗi in của nó.__str__ is the 2nd most commonly used operator overloading in Python after __init__. The __str__ is run automatically whenever an instance is converted to its print string. __str__ is the 2nd most commonly used operator overloading in Python after __init__. The __str__ is run automatically whenever an instance is converted to its print string.

Hãy sử dụng ví dụ trước:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
01

Nếu chúng ta in phiên bản, nó sẽ hiển thị toàn bộ đối tượng như hình dưới đây.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
02

Nó hiển thị tên lớp của đối tượng và địa chỉ của nó trong bộ nhớ về cơ bản là vô dụng trừ khi là một định danh duy nhất.

Vì vậy, hãy thêm phương thức __str__:__str__ method:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
03

Mã trên mở rộng lớp của chúng tôi để cung cấp một màn hình tùy chỉnh liệt kê các thuộc tính khi các phiên bản của lớp chúng tôi được hiển thị toàn bộ, thay vì dựa vào màn hình ít hữu ích hơn. Lưu ý rằng chúng tôi đang thực hiện định dạng chuỗi % để xây dựng chuỗi hiển thị trong __str__.__str__.

__str__ vs __repr__

Sự khác biệt giữa __str__ và __repr__ không rõ ràng.__str__ and __repr__ are not that obvious.

Khi chúng tôi sử dụng in, Python sẽ tìm kiếm một phương thức __str__ trong lớp của chúng tôi. Nếu nó tìm thấy một, nó sẽ gọi nó. Nếu không, nó sẽ tìm kiếm một phương thức __repr__ và gọi nó. Nếu nó không thể tìm thấy một, nó sẽ tạo ra một đại diện nội bộ của đối tượng của chúng tôi.__str__ method in our class. If it finds one, it will call it. If it does not, it will look for a __repr__ method and call it. If it cannot find one, it will create an internal representation of our object.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
04

Không có nhiều thông tin từ in (x) và chỉ lặp lại đối tượng x. Đó là lý do tại sao chúng tôi tùy chỉnh lớp bằng cách sử dụng __str__.print(x) and just echoing the object x. That's why we do customize the class by using __str__.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
05

Nhưng không phải khi chúng ta sử dụng in (myobjecs). Lưu ý rằng các phiên bản nằm trong danh sách:print(myObjecs). Note that the instances are in the list:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
06

Vì vậy, chúng ta cần xác định __repr__:__repr__:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
07

Trong cuốn sách của mình, Learning Python, Mark Lutz tóm tắt như sau: ... repr__, cung cấp một màn hình cấp thấp của mã hóa của một đối tượng khi có mặt. Đôi khi các lớp cung cấp cả __str__ cho màn hình thân thiện với người dùng và __repr__ với các chi tiết bổ sung cho các nhà phát triển để xem. Bởi vì in chạy __str__ và lời nhắc tương tác lặp lại kết quả với __repr__, điều này có thể cung cấp cho cả đối tượng mục tiêu một màn hình phù hợp.
...__repr__, provides an as-code low-level display of an object when present. Sometimes classes provide both a __str__ for user-friendly displays and a __repr__ with extra details for developers to view. Because printing runs __str__ and the interactive prompt echoes results with __repr__, this can provide both target audience with an appropriate display.

sys.argv

sys.argv là danh sách các lập luận được chuyển cho chương trình Python. Đối số đầu tiên, sys.argv [0], thực sự là tên của chương trình. Nó tồn tại để chúng ta có thể thay đổi hành vi của chương trình tùy thuộc vào cách nó được gọi. Ví dụ: is the list of arguments passed to the Python program.
The first argument, sys.argv[0], is actually the name of the program. It exists so that we can change our program's behavior depending on how it was invoked. For example:

  1. sys.argv [0] là hệ điều hành phụ thuộc vào việc đây có phải là một tên đường dẫn đầy đủ hay không. is operating system dependent whether this is a full pathname or not.
  2. Nếu lệnh được thực thi bằng tùy chọn dòng lệnh -c cho trình thông dịch, sys.argv [0] sẽ được đặt thành chuỗi '-c'.-c command line option to the interpreter, sys.argv[0] is set to the string '-c'.
  3. Nếu không có tên tập lệnh nào được chuyển cho trình thông dịch Python, sys.argv [0] là chuỗi trống.sys.argv[0] is the empty string.

Do đó, Sys.Argv [1] là đối số đầu tiên chúng tôi thực sự chuyển sang chương trình. is thus the first argument we actually pass to the program.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
08

Nếu chúng ta chạy nó có hoặc không có bất kỳ đối số nào, chúng ta sẽ nhận được đầu ra bên dưới:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
09 getOpt.getOpt (args, Tùy chọn [, long_options])

getopt.getopt(args, options[, long_options])

GetOpt.GetOpt () phân tích các tùy chọn dòng lệnh và danh sách tham số. Args là danh sách đối số sẽ được phân tích cú pháp, mà không cần tham chiếu hàng đầu đến chương trình đang chạy. Thông thường, điều này có nghĩa là sys.argv [1:]. Tùy chọn là chuỗi các chữ cái tùy chọn mà tập lệnh muốn nhận ra, với các tùy chọn yêu cầu một đối số theo sau là một dấu hai chấm (:).getopt.getopt() parses command line options and parameter list. args is the argument list to be parsed, without the leading reference to the running program. Typically, this means sys.argv[1:]. options is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (:).

long_options, nếu được chỉ định, phải là một danh sách các chuỗi có tên của các tùy chọn dài cần được hỗ trợ. Các ký tự hàng đầu - không nên được đưa vào tên tùy chọn. Các tùy chọn dài yêu cầu một đối số phải được theo sau bởi một dấu hiệu bằng nhau (=). Đối số tùy chọn không được hỗ trợ. Để chỉ chấp nhận các tùy chọn dài, các tùy chọn phải là một chuỗi trống. Các tùy chọn dài trên dòng lệnh có thể được nhận ra miễn là chúng cung cấp một tiền tố của tên tùy chọn khớp chính xác một trong các tùy chọn được chấp nhận: ví dụ, nếu long_options là ['foo', 'frob'], tùy chọn - -foo Sẽ phù hợp như - -foo, nhưng - -f sẽ không khớp với duy nhất, vì vậy getopterror sẽ được nâng lên., if specified, must be a list of strings with the names of the long options which should be supported. The leading -- characters should not be included in the option name. Long options which require an argument should be followed by an equal sign (=). Optional arguments are not supported. To accept only long options, options should be an empty string. Long options on the command line can be recognized so long as they provide a prefix of the option name that matches exactly one of the accepted options:
For example, if long_options is ['foo', 'frob'], the option --fo will match as --foo, but --f will not match uniquely, so GetoptError will be raised.

Giá trị trả về bao gồm hai yếu tố:return value consists of two elements: return value consists of two elements:

  1. Đầu tiên là danh sách các cặp (tùy chọn, giá trị)
  2. Thứ hai là danh sách các đối số chương trình còn lại sau khi danh sách tùy chọn bị tước (đây là một lát cắt của Args).

Mỗi cặp tùy chọn và giá trị được trả về có tùy chọn làm phần tử đầu tiên của nó, được đặt trước với dấu gạch nối cho các tùy chọn ngắn (ví dụ: '-x') hoặc hai dấu gạch nối cho các tùy chọn dài (ví dụ: '-Long-exption') và Đối số tùy chọn là phần tử thứ hai của nó hoặc một chuỗi trống nếu tùy chọn không có đối số. Các tùy chọn xảy ra trong danh sách theo cùng một thứ tự mà chúng được tìm thấy, do đó cho phép nhiều lần xuất hiện. Các tùy chọn dài và ngắn có thể được trộn lẫn. -From http://docs.python.org/2/l Library/getopt.html. -from http://docs.python.org/2/library/getopt.html.
-from http://docs.python.org/2/library/getopt.html.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
0

Nếu chúng ta chạy với hai tùy chọn dài tệp:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
1

Với các tùy chọn ngắn:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
2

Lưu ý rằng tùy chọn dài không phải khớp đầy đủ:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
3 __call__

__call__

Python chạy một phương thức __call__ khi một thể hiện được gọi là một dạng hàm. Điều này hữu ích hơn khi chúng tôi thực hiện một số công việc giao thoa với API mong đợi các chức năng. Trên hết, chúng ta cũng có thể giữ lại thông tin trạng thái như chúng ta thấy trong các ví dụ sau của phần này.__call__ method when an instance is called as a function form. This is more useful when we do some interfacing work with APIs expecting functions. On top of that, we can also retain state info as we see in the later examples of this section.__call__ method when an instance is called as a function form. This is more useful when we do some interfacing work with APIs expecting functions. On top of that, we can also retain state info as we see in the later examples of this section.

Nó được cho là phương pháp quá tải toán tử được sử dụng phổ biến thứ ba, đằng sau __init__ và __str__ và __repr__ theo Mark Lutz (học Python).__init__ and the __str__ and __repr__ according to Mark Lutz (Learning Python).__init__ and the __str__ and __repr__ according to Mark Lutz (Learning Python).

Dưới đây là một ví dụ với phương thức __call__ trong một lớp. Phương thức __call__ chỉ đơn giản là in ra các đối số mà nó thực hiện thông qua từ khóa args.

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
4

Như một ví dụ khác với nhiều đối số hỗn hợp hơn một chút:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
5

Mặc dù phương thức __call__ cho phép chúng tôi sử dụng các phiên bản lớp để mô phỏng các hàm như chúng tôi đã thấy trong các ví dụ trên, có một cách sử dụng khác của phương thức __call__: chúng tôi có thể giữ lại thông tin trạng thái:__call__ method allows us to use class instances to emulate functions as we saw in the above examples, there is another use of __call__ method: we can retain state info:__call__ method allows us to use class instances to emulate functions as we saw in the above examples, there is another use of __call__ method: we can retain state info:

class Student(object):
        '''Classes can (and should) have docstrings too, just like modules and functions'''
	def __init__(self, name, id = 20001):
		self.name = name
		self.id = id
6

Làm thế nào để bạn chuyển một cuộc tranh luận cho một lớp học trong Python?

Một hàm có thể lấy nhiều đối số, các đối số này có thể là đối tượng, biến (có cùng loại dữ liệu hoặc khác nhau) và các hàm.Các hàm Python là đối tượng hạng nhất.Trong ví dụ dưới đây, một hàm được gán cho một biến., these arguments can be objects, variables(of same or different data types) and functions. Python functions are first class objects. In the example below, a function is assigned to a variable., these arguments can be objects, variables(of same or different data types) and functions. Python functions are first class objects. In the example below, a function is assigned to a variable.

Lớp Python có thể lấy lập luận không?

Bất kỳ phương pháp lớp nào cũng phải có bản thân như đối số đầu tiên... (The name can be any valid variable name, but the name self is a widely established convention in Python.) self represents an (arbitrary) instance of the class.. (The name can be any valid variable name, but the name self is a widely established convention in Python.) self represents an (arbitrary) instance of the class.

Làm thế nào để bạn chuyển một biến cho một lớp khác trong Python?

Nhận một biến từ lớp A, sử dụng đối tượng của nó.Chuyển nó như một đối số cho một hàm của lớp B (truy cập hàm này bằng cách sử dụng đối tượng của lớp B).Hàm này sẽ chuyển giá trị của biến này cho một biến trong lớp B.Pass it as an argument to a function of class B (access this function using object of class B). This function should pass on the value of this variable to a variable in class B.Pass it as an argument to a function of class B (access this function using object of class B). This function should pass on the value of this variable to a variable in class B.

Bạn có thể chuyển một lớp cho một python chức năng không?

Có của thô, bạn có thể vượt qua các lớp hoặc chức năng hoặc thậm chí các mô -đun ... def foo (): Truyền ở đây nó cung cấp địa chỉ cho Ex. ... def foo(): pass here it gives it address for ex. ... def foo(): pass here it gives it address for ex.