Python Forum
Importing issues with base class for inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing issues with base class for inheritance
#5
(May-19-2021, 04:26 PM)Gribouillis Wrote: To understand how it works you need to have some basic notions of how import statements work. When it executes say import food_bot.price, Python looks in the dictionary sys.modules if it already contains a module named food_bot.price. If it is not the case, a new empty module object is created an inserted immediately in sys.modules. Then Python starts executing in this module's namespace the code that in finds in __init__.py. There it encounters the line from .pricing import Pricing. It inserts immediately a module named food_bot.price.pricing in sys.modules and it executes the code of pricing.py in that module's namespace. After that, this module contains the class named Pricing and Python can complete the import. At that point, the module food_bot.price exists and contains the name Pricing.

Then Python runs from .item_pricing import ItemPricing. This executes the code from item_pricing.py in a new module's namespace. That code contains the line from . import Pricing. The . module here is food_bot.price which is already in sys.modules and already contains the symbol Pricing. Hence the line suceeds.

Thanks a lot!
Reply


Messages In This Thread
RE: Importing issues with base class for inheritance - by riccardoob - May-19-2021, 04:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 1,025 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Some help with my first python class and importing ....im making a lidar program jttolleson 7 1,480 Jul-28-2023, 08:34 AM
Last Post: jttolleson
  My code displays too much output when importing class from a module lil_e 4 1,365 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Child class inheritance issue eakanathan 3 1,508 Apr-21-2022, 12:03 PM
Last Post: deanhystad
  Calling a base class variable from an inherited class CompleteNewb 3 1,904 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  3D vector class with inheritance from 2D vector class buss0140 4 3,354 Dec-20-2020, 08:44 PM
Last Post: deanhystad
  Class inheritance oclmedyb 3 2,402 Dec-09-2020, 04:43 PM
Last Post: deanhystad
  Can we access instance variable of parent class in child class using inheritance akdube 3 14,211 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Behavior of Abstract Base Class dgrunwal 4 2,371 Oct-15-2020, 07:19 PM
Last Post: Gribouillis
  newbie question....importing a created class ridgerunnersjw 5 2,852 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020