A memo of misunderstanding when trying to load the entire self-made module with Python3

what's happened

Note the misunderstanding when trying to read multiple classes (Apple, Orange, Grape) in a self-made module (fruits.py) with ʻimport fruits`. Reference: http://www.python-izm.com/contents/basis/import.shtml

This misunderstanding

Suppose you have the following modules.

fruits.py


class Apple():
  def hoge():
    pass

class Orange():
  def hoge():
    pass

class Grape():
  def hoge():
    pass

When I read the class in this module with ʻimport fruits` and tried to create an instance, the following error occurred.

>>> import fruits
>>> apple = Apple()
                                                                                            
>>> type(apple)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Apple' is not defined  

Correctly, treat it as module name.class. The cause of this misunderstanding was that I tried to treat it as type (class name) with the ʻimport module`.

>>> import fruits    
>>> type(fruits.Apple)
<class 'type'>   

If you want to treat it like type (class name), do as follows.

>>> from fruit import Apple
>>> type(Apple)
<Class 'type'>

Postscript

We received a comment from shiracamus. If you want to make it like type (class), you can write from module import *.

>>> from fruit import *
>>> type(Apple)
<class 'type'>

Recommended Posts

A memo of misunderstanding when trying to load the entire self-made module with Python3
A memo connected to HiveServer2 of EMR with python
[python] A note when trying to use numpy with Cython
Get the source of the page to load infinitely with python.
The story of making a module that skips mail with python
Create a compatibility judgment program with the random module of python.
A story that didn't work when I tried to log in with the Python requests module
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
Error when installing a module with Python pip
A memo when creating a python environment with miniconda
[Python] A rough understanding of the logging module
The story of making a tool to load an image with Python ⇒ save it as another name
When using PyQtGraph with Python Pyside, pay attention to the order of import
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
A memo to visually understand the axis of pandas.Panel
[Python] A memo to write CSV vertically with Pandas
A memo that I touched the Datastore with python
Python Note: The mystery of assigning a variable to a variable
A story that got stuck when trying to upgrade the Python version on GCE
How to identify the element with the smallest number of characters in a Python list?
I got stuck when trying to specify a relative path with relative_to () in python
A memo on how to overcome the difficult problem of capturing FX with AI
A story about trying to introduce Linter in the middle of a Python (Flask) project
I tried to find the entropy of the image with python
[Ubuntu] How to delete the entire contents of a directory
Make the display of Python module exceptions easier to understand
Minimum knowledge to get started with the Python logging module
Probably the easiest way to create a pdf with Python3
A memo when face is detected with Python + OpenCV quickly
How to develop in a virtual environment of Python [Memo]
[Python] The first step to making a game with Pyxel
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
A program that failed when trying to create a linebot with reference to "Dialogue system made with python"
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
[Python, Selenium, PhantomJS] A story when scraping a website with lazy load
How to determine the existence of a selenium element in Python
[python, ruby] fetch the contents of a web page with selenium-webdriver
[Introduction to Python] How to split a character string with the split function
A story that struggled to handle the Python package of PocketSphinx
How to check the memory size of a variable in Python
[Python] A memo that I tried to get started with asyncio
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
Output the contents of ~ .xlsx in the folder to HTML with Python
[Python] A memo to operate ROM created by GBDK with PyBoy
I tried to create a list of prime numbers with python
Let's stop copying. Introducing flati, a module to flatten with Python
[Python3] Define a decorator to measure the execution time of a function
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
The idea of feeding the config file with a python file instead of yaml
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Prepare a distributed load test environment with the Python load test tool Locust
How to deal with the error "Failed to load module" canberra-gtk-module "that appears when you run OpenCV
I tried to improve the efficiency of daily work with Python
Try it when Visual Studio Code can't load a Python module
How to send a request to the DMM (FANZA) API with python
[python] A note that started to understand the behavior of matplotlib.pyplot
The story of the algorithm drawing a ridiculous conclusion when trying to solve the traveling salesman problem properly
[Python] A simple function to find the center coordinates of a circle
[Python] A program that rotates the contents of the list to the left