[PythonEdu] How to create a module in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 33

[PythonEdu] How to create a module in Python?

Inspired by one of the threads in Q&A recently, I prepared a PythonEdu code on how to create own modules in Python to later be used (imported) in other programs. Of course, for building proper structures a whole lot of conventions and PEPs are applied. This is just an example on how simple it is to do. Have fun watching :) https://code.sololearn.com/cLF0P3C3eZ52/?ref=app https://www.sololearn.com/discuss/645722/?ref=app

21st Aug 2017, 10:44 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
13 Answers
+ 16
It's THAT simple? I thought Python modules were created with C!
21st Aug 2017, 11:11 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
@David Ah, yes, the powerful dir() method. It lists all attributes of its argument (an object) or the list of names in the local scope, if called plain. Lets you know what can you do with this object, a bit.
22nd Aug 2017, 4:47 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
@00-00-72 may I ask how that contributes to the discussion here?
23rd Aug 2017, 6:00 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
@Kuba, I added these lines to your code: import my_module print(dir(my_module)) I expected to get: ['my_method'] but instead got: ['__builtins__', '__cached__', '__doc__', '__file__' '__loader__, '__name__', '__package__', '__spec__', 'my_method'] How cool is that! 😊
22nd Aug 2017, 2:27 AM
David Ashton
David Ashton - avatar
+ 2
@sayan chandra listdir() is a method of the os module so print(math.listdir()) will give an error.
23rd Aug 2017, 1:00 AM
David Ashton
David Ashton - avatar
+ 2
Srsly? That doesn't contribute much here? We are talking about making modules and you are using a PSL module
23rd Aug 2017, 6:05 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
@00-00-72 Have you been able to import turtle into the SL code playground? When I've tried it, I get a "memory exceeded" message 😐
23rd Aug 2017, 6:05 AM
David Ashton
David Ashton - avatar
23rd Aug 2017, 7:27 AM
李立威
+ 1
creating a simple module is like writing a sript and if you want to use it in interpreter you have import it using program name
23rd Aug 2017, 2:19 AM
santhosh
+ 1
i don't know
23rd Aug 2017, 2:46 AM
Bình Nguyễn Văn
Bình Nguyễn Văn - avatar
0
very nice... @ DAVID ASHTON I TRIED THAT ALSO AND GOT WHAT YOU SAID... lokks like those builtins,cathed,name,etc those are created by default if you creat a module..... @ David//@ kuba tell me... suppose i imported math.....thennnnn print(math.listdir()) and print(dir(math)) are same right...?
22nd Aug 2017, 2:43 PM
sayan chandra
sayan chandra - avatar
- 1
import any module of Python from Python library file
23rd Aug 2017, 2:47 AM
Amaan Memon
Amaan Memon - avatar
- 4
hi
22nd Aug 2017, 4:33 PM
Saumya R. Tongaria
Saumya R. Tongaria - avatar