Want to guarantee that no one can run "from .. import *" on your #Python module? Include a non-existent name in the __all__ list: x = 100 y = [10, 20, 30] __all__ = ['YOU_SHOULD_NOT_USE_IMPORT_*'] Using "from .. import *" on this module will result in an error!
Prevent Import Errors in Python with __all__
More Relevant Posts
-
"Now let me build this calculator. This is a substantial Python script — I'll create the full workbook with all sheets, formulas, and formatting in one go." Isn't this world we live in kind of crazy now?
To view or add a comment, sign in
-
How are arguments assigned to parameters when calling a #Python function? There are two ways: - Positional, based on order - Keyword, i.e., name=value, assigned by name Want to mix and match? Fine, but all positional must come before all keyword: myfunc(10, 20, c=30)
To view or add a comment, sign in
-
-
Python didn’t throw an error… and that’s the problem x = 10 x = "10" Later: print(x + 5) This crashes. Not when the mistake happened… but later, when the variable was actually used. That’s the difference. Python lets you change types freely (dynamic typing), but errors only show up at runtime. In C++, this wouldn’t even compile. You’d catch it immediately. So the real question is: Do you want errors early… or flexibility first and consequences later? Day 3/30 #Python #C++ #LearningInPublic #30DaysOfCode
To view or add a comment, sign in
-
-
Want to include a dynamic value in a multi-line #Python string? That would require both an f-string and a triple-quoted string. Good news: Python supports this! x = 10 y = 20 s = f"""{x} + {y} = {x+y} {x} * {y} = {x*y}""" print(s) # shows 10+20 = 30 and also 10*20 = 200
To view or add a comment, sign in
-
-
Python one-liner I use to feel like a wizard: Turn list of tuples into dict without loops: data = [('id', 12), ('name', 'Ash'), ('coffee', 'yes please')] d = dict(data) print(d) # {'id': 12, 'name': 'Ash', 'coffee': 'yes please'} Bonus: if keys duplicate → last value wins (no error) #Python #CodingHumor #OneLiners #TechTips #MomInTech #WomenInTech #LearningEveryday
To view or add a comment, sign in
-
Any #Python function parameters after *args are keyword-only: def myfunc(*args, k): return f'{args=}, {k=}' Want to give k a value? Use a keyword argument: myfunc(10, 20, 30, k=100) Don't need *args? Just use *, and k is keyword only: def myfunc(*, k): return f'{k=}'
To view or add a comment, sign in
-
-
1.Number Increasing Pattern using Python loops. Code: s = 5 for i in range(1, s+1): for j in range(1, 1+i): print(j, end=" ") print() Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 2.Star Triangle Pattern using Python and nested loops. Python Code: n = 6 for i in range(1, n+1): for j in range(1, i+1): print('*', end=" ") print() Output: * * * * * * * * * * * * * * * * * * * * *
To view or add a comment, sign in
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Alternatively __all__ = [] will allow from module import * , but no names will be added to the global name space.