Nhập lớp con Python

riccardoob
Người Pháp ngớ ngẩn

Bài đăng. 21

Chủ đề. 7

Đã tham gia. Tháng 9 năm 2019

Danh tiếng. 0

19-May-2021, 12. 44 PM [Bài đăng này đã được sửa đổi lần cuối. May-19-2021, 03. 50 giờ tối bởi riccardoob. ]

Xin chào, tôi đang cố tạo một lớp kế thừa từ một lớp khác. Vấn đề tôi gặp phải liên quan đến nhập khẩu, tôi sẽ cố gắng giải thích tốt nhất có thể.

food_bot
├── __init__. py
├── giá
│   ├── __init__. py
│   ├── item_pricing. py
│   └── định giá. py

Đây là cấu trúc thư mục của dự án [chưa hoàn chỉnh, chỉ có thư mục giá phù hợp với vấn đề của tôi].
Trong __init__. py giá bên trong tôi đã nhập

from .item_pricing import ItemPricing
from .pricing import Pricing

__all__ = [
    'ItemPricing',
    'Pricing'
]
Tôi cần nhập Giá trong tệp item_pricing. py và tôi đã làm điều đó bằng cách sử dụng đường dẫn đầy đủ đến lớp Pricing.
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
Tôi đã làm điều này bởi vì, khi nhập Định giá như thế này từ food_bot. nhập giá Định giá [và không phải như thế này từ food_bot. giá bán. định giá nhập khẩu Định giá] bắt đầu nhập khẩu tuần hoàn; .
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
8 mặc dù nó được xác định.

Tóm lại, tôi muốn có thể nhập Định giá như thế này,
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
9 vì đây là mục đích của __init__ của tôi. py VÀ sử dụng Pricing làm lớp cơ sở cho ItemPricing.
Tôi có thể không hiểu rõ TYPE_CHECKING thực sự hữu ích cho việc gì [điều tương tự cho __future__. chú thích], tôi đã thử đọc tài liệu nhưng có vẻ như tôi đã hiểu sai.
Ai đó có thể giúp tôi không?

Hồi đáp

Tìm thấy

Hồi đáp

Gribouillis

Bài viết. 3.855

Chủ đề. 56

Đã tham gia. Tháng 1 năm 2018

Danh tiếng. 304

May-19-2021, 02. 55 giờ chiều

Bạn có thể nhập giá trước item_pricing trong init. py
______2thì trong item_pricing. py

# pricing/item_pricing.py
from . import Pricing

class ItemPricing[Pricing]:
    ....
Tôi không thể trả lời về TYPE_CHECKING vì tôi không sử dụng nó. Chính từ này có vẻ antipythonic đối với tôi.

Hồi đáp

Tìm thấy

Hồi đáp

riccardoob
Người Pháp ngớ ngẩn

Bài đăng. 21

Chủ đề. 7

Đã tham gia. Tháng 9 năm 2019

Danh tiếng. 0

May-19-2021, 03. 05 giờ chiều

[19-05-2021, 02. 55 giờ chiều] Gribouillis đã viết. Bạn có thể nhập giá trước item_pricing trong init. py
______2thì trong item_pricing. py
# pricing/item_pricing.py
from . import Pricing

class ItemPricing[Pricing]:
    ....
Tôi không thể trả lời về TYPE_CHECKING vì tôi không sử dụng nó. Chính từ này có vẻ antipythonic đối với tôi.

Tôi đã thử và nó hoạt động. Tại sao điều này làm việc mặc dù?

Hồi đáp

Tìm thấy

Hồi đáp

Gribouillis

Bài viết. 3.855

Chủ đề. 56

Đã tham gia. Tháng 1 năm 2018

Danh tiếng. 304

May-19-2021, 04. 26 giờ chiều

Để hiểu cách thức hoạt động của nó, bạn cần có một số khái niệm cơ bản về cách hoạt động của câu lệnh nhập. Khi nó thực thi nói

from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
0, Python sẽ tìm trong từ điển
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
1 nếu nó đã chứa mô-đun có tên
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
2. Nếu không phải như vậy, một đối tượng mô-đun trống mới được tạo và chèn ngay vào
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
1. Sau đó, Python bắt đầu thực thi trong không gian tên của mô-đun này mã tìm thấy trong
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
4. Ở đó, nó gặp dòng
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
5. Nó ngay lập tức chèn một mô-đun có tên là
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
6 vào trong
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
1 và nó thực thi mã của
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
8 trong không gian tên của mô-đun đó. Sau đó, mô-đun này chứa lớp có tên Định giá và Python có thể hoàn tất quá trình nhập. Tại thời điểm đó, mô-đun
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
2 tồn tại và chứa tên
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
0.

Sau đó, Python chạy
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
1. Điều này thực thi mã từ
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
2 trong không gian tên của mô-đun mới. Mã đó chứa dòng
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
3. Mô-đun
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
4 ở đây là
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
2 đã có trong
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
1 và đã chứa ký hiệu
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
0. Do đó dòng thành công.

snipsat thích bài viết này

Hồi đáp

Tìm thấy

Hồi đáp

riccardoob
Người Pháp ngớ ngẩn

Bài đăng. 21

Chủ đề. 7

Đã tham gia. Tháng 9 năm 2019

Danh tiếng. 0

May-19-2021, 04. 46 CH

[19-05-2021, 04. 26 giờ chiều] Gribouillis đã viết. Để hiểu cách thức hoạt động của nó, bạn cần có một số khái niệm cơ bản về cách thức hoạt động của các câu lệnh nhập. Khi nó thực thi nói
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
0, Python sẽ tìm trong từ điển
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
1 nếu nó đã chứa mô-đun có tên
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
2. Nếu không phải như vậy, một đối tượng mô-đun trống mới được tạo và chèn ngay vào
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
1. Sau đó, Python bắt đầu thực thi trong không gian tên của mô-đun này mã tìm thấy trong
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
4. Ở đó, nó gặp dòng
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
5. Nó ngay lập tức chèn một mô-đun có tên là
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
6 vào trong
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
1 và nó thực thi mã của
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
8 trong không gian tên của mô-đun đó. Sau đó, mô-đun này chứa lớp có tên Định giá và Python có thể hoàn tất quá trình nhập. Tại thời điểm đó, mô-đun
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
2 tồn tại và chứa tên
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
0.

Sau đó, Python chạy
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
1. Điều này thực thi mã từ
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
2 trong không gian tên của mô-đun mới. Mã đó chứa dòng
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
3. Mô-đun
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
4 ở đây là
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
2 đã có trong
from __future__ import annotations
from math import floor
from typing import TYPE_CHECKING

from food_bot.menu import MenuItem
from food_bot.exceptions import ParameterError
from food_bot.price.pricing import Pricing

class ItemPricing[Pricing]: ...
1 và đã chứa ký hiệu
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
0. Do đó dòng thành công.

Cảm ơn rất nhiều

Hồi đáp

Tìm thấy

Hồi đáp

đoạn trích

Bài viết. 6.528

Chủ đề. 116

Đã tham gia. Tháng 9 năm 2016

Danh tiếng. 486

May-19-2021, 05. 18 giờ chiều [Bài đăng này đã được sửa đổi lần cuối. May-19-2021, 05. 18 giờ tối bởi snippsat. ]

Cũng có thể xem cái này.
Xem trong liên kết cách tôi thực hiện đơn giản hóa

# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
6[tạm biệt các mô-đun phụ], vì vậy có thể nhập đơn giản hơn.

Ví dụ khi bạn sử dụng Yêu cầu, quá trình nhập là
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
7[chỉ vậy thôi].
Sau đó, với lệnh gọi thuộc tính đơn giản, hãy sử dụng nó
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
8

Tôi cho rằng điều này sẽ cố gắng làm cho
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
6 trở nên đơn giản đối với người dùng, sau đó không cần điều hướng cây tệp của bạn bằng 3 .
Thông thường, tạo gói là bước cuối cùng mà hầu hết mọi người chưa xem xét kỹ trước khi dùng thử, khi đó kết quả có thể không tối ưu, chẳng hạn như bên dưới😵
# pricing/__init__.py
from .pricing import Pricing
from .item_pricing import ItemPricing
6

Hồi đáp

Tìm thấy

Hồi đáp

Bạn có thể nhập các lớp bằng Python không?

Python cung cấp cho chúng ta nhiều cách khác nhau để chúng ta có thể nhập các lớp và hàm bằng cách sử dụng câu lệnh nhập .

Lớp con trong Python là gì?

Đó là khả năng của một lớp kế thừa hoặc kế thừa các thuộc tính từ một số lớp khác . Nó cũng cung cấp khả năng sử dụng lại mã. Chúng ta không phải viết đi viết lại cùng một mã. Ngoài ra, nó cho phép chúng tôi thêm nhiều tính năng hơn vào một lớp mà không cần sửa đổi nó.

__Import__ trong Python là gì?

Người ta có thể sử dụng hàm __import__[] có sẵn của Python. Việc nhập các mô-đun trong thời gian chạy cũng giúp . cú pháp. __import__[tên, toàn cầu, địa phương, danh sách từ, cấp độ] Tham số. Tên. Tên của mô-đun được nhập.

Tôi có thể nhập __ init __ không?

Có hai loại gói trong python, gói thông thường và gói không gian tên. Cái trước yêu cầu __init__. py trong khi cái sau thì không. Bất kỳ thư mục nào có tệp init python đều được python đánh dấu là gói và có thể được nhập .

Chủ Đề