Python Forum
Calls to Attributes of a Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calls to Attributes of a Class
#1
how does a direct call to openpyxl.load_workbook work?

According to OpenPyXL documentation, load_workbook is a submodule of a the subpackage reader i.e:
openpyxl.reader.excel.load_workbook

However a direct call to openpyxl.load_workbook works fine while using other submodules of openpyxl need the full callout. See below:

Initially in my code I had:
import openpyxl
try:
    openpyxl.load_workbook(filename)
except openpyxl.InvalidFileException as ife:
    print(ife)
which gave me the following error:
Error:
except openpyxl.InvalidFileException as ife: AttributeError: module 'openpyxl' has no attribute 'InvalidFileException'
however, when I changed line 4 to:
except openpyxl.utils.exceptions.InvalidFileException as ife:
exception handling work without an error.

Why full Callout required on one and not the other submodule? I could not find anything in the Documentation.
Reply
#2
it depends what names are available on top level

>>> import openpyxl
>>> dir(openpyxl)
['DEFUSEDXML', 'LXML', 'NUMPY', 'Workbook', '__author__', '__author_email__', '__builtins__', '__cached__', '__doc__', '__file__', 
'__license__', '__loader__', '__maintainer_email__', '__name__', '__package__', '__path__', '__spec__', '__url__', 
'__version__', '_constants', 'cell', 'chart', 'chartsheet', 'comments', 'compat', 
'constants', 'descriptors', 'drawing', 'formatting', 'formula', 'load_workbook', 
'open', 'packaging', 'pivot', 'reader', 'styles', 'utils', 'workbook', 'worksheet', 'writer', 'xml']
load_workbook is made available/is exposed on top level

Here is the __init__.py source code
https://foss.heptapod.net/openpyxl/openp..._init__.py

In other words - it's a decision of the developer how to organise their code and what names to be exposed top level.
SKarimi likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Also, note that open is alias for load_workbook
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks buran. Exactly what I was looking for. Thumbs Up
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SQL Alchemy dynamic class - declarative_base losing attributes mrdominikku 4 2,011 Jan-10-2020, 06:46 PM
Last Post: mrdominikku
  Need help with a function that calls other functions. skurrtboi 4 1,158 Sep-30-2019, 09:28 PM
Last Post: stullis
  how to add class instance attributes from list 999masks 2 1,484 Jul-22-2019, 07:59 AM
Last Post: 999masks
  Testing function calls jenselme 1 1,583 Jul-25-2018, 10:33 AM
Last Post: Larz60+
  function state between calls Skaperen 5 3,469 Feb-08-2018, 02:20 AM
Last Post: Skaperen
  Is it possible to loop through class attributes via string? 04chiak 3 8,205 Feb-04-2018, 09:29 PM
Last Post: 04chiak
  Is there another way to do a bunch of methods calls ? Windspar 7 3,018 Jan-02-2018, 06:26 PM
Last Post: Windspar
  Class Instances overriding class members and attributes. Leaf 7 4,499 Nov-29-2017, 06:08 AM
Last Post: Leaf
  Class Attributes Inheritance Harry_Potter 3 2,485 Nov-16-2017, 07:01 PM
Last Post: snippsat
  Function calls inside a Class kerzol81 2 2,426 Mar-05-2017, 06:45 PM
Last Post: kerzol81

Forum Jump:

User Panel Messages

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