Weird way of importing Python turtle works but I don't know why - python-3.x

I hope you will understand my question.
I noticed that I get the same results when I import turtle module in following two ways.
from turtle import Turtle
t=Turtle()
t.screen.bgcolor("black")
and also
import turtle
turtle.bgcolor("black")
I am confused about this, “from turtle import Turtle”.
According to what I know, it means “to import Turtle.py from turtle (folder / package)”. I may be wrong, you can help me out to understand better.
But I can’t find any Turtle.py module. It is only turtle.py I saw.
What's weird about it is that it works.
Can anyone tell me why?
I am using Python version 3.6

Python's turtle.py is unusual in that it presents both a function-based interface and an object-oriented interface. Depending on how you import it, you can work with one, or the other, or both.
Here, we are using the object-oriented interface to invoke the screen method bgcolor():
from turtle import Turtle
t = Turtle()
t.screen.bgcolor("black")
I usually write this as:
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor("black")
t = Turtle()
as having direct access to the Screen object simplifies things. Using this style import, you cannot access the function-based interface.
When we do this simpler import, we have access to both the function-based interface and the object-oriented interface. Here, we're using the function bgcolor() to set the background color:
import turtle
turtle.bgcolor("black")
Using either the function-based or object-oriented interface to turtle.py is fine, but you can get yourself seriously confused when mixing the two.

Related

Access mesh imported with gmsh

I think I have a very common problem. Can you help me out? I want to examine a 3D-mesh with python. I want to use trimesh in order to examine the mesh but the mesh comes with the .STEP Format. I use gmsh to load the mesh but I have no idea how I can access the mesh I have or how I can convert it into a trimesh.mesh.
Can you help me out?
My code looks like this so far:
import trimesh
import gmsh
import pygmsh
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 1)
gmsh.model.add("modelo_1")
gmsh.merge(
"C:/Users/PythonFan/RandomFile.STEP")
gmsh.model.mesh.generate(3)
[...]
x.center_mass
So how do I get from here to there?
I found I what I have to do :D
x = trimesh.Trimesh(**trimesh.interfaces.gmsh.load_gmsh("C:/Users/....STEP")
This allows me to use use the gmsh loading abilities while examining the object with trimesh. I'll post my source once I found it again.

Turtle is not running

import turtle
t = turtle.Turtle()
t.right(100)
t.done()
That is my code, but there is no result after run. Why this problem is arising and what can I do now?
t.right(100)
this rotates the turtle by 100 degrees
t.done()
is not a function. All your code is doing essentially is creating a canvas and putting nothing on it. From here, you would need to know about t.pendown() and other functions to make it work this link will be helpful in getting started with the python turtle.

importing custom module , don't load other import from the module

I'm trying to build some custom Python modules.
Even though I was able to use all modules. When I try to import a module other modules are being imported.
For example:
mod1.py
import os
import sys
def f(x):
return (x**2)
main.py
import mod1
dir(mod1.os)
how can I avoid this behaviour? The user should not be able to access the other modules from mod1. In this example os and sys.
Should I put the import statements inside the function? Are there other ways to prevent such thing?
The behavior you are seeing is perfectly normal.
Python does not hide elements quite so strongly as some other languages.
We're all adults here, and are expected to behave responsibly, without strong enforcement from the language.
Should I put the import statements inside the function?
No, your code is just fine the way it is.
If you plan to write a bunch of python code, you should follow the usual conventions that everyone else does, as shown in your example code. In main.py, if you want to access an os function, you should certainly import it there, rather than borrowing a reference from mod1.os.

PyQt and PyQtGraph library. qtgui.QGraphicsObject vs pyqtgraph.GraphicsObject

I'm not sure I understand how these work (I'm not even sure if I should talk about these or this). I was reading pyqtgraph examples and found a call to a "GraphicsObject". Looked it up in pyqtgraph's documentation and it seems like they have their own QGraphicsObject class. Is that correct?
I can't manage to import the correct GraphicsObject (amongst others, like GraphicsItem and such; just using that class as an attempt to explain myself better). I've reinstalled the library so it has nothing to do with it, so... what I'm missing? I apologize in advance if the question is silly.
Example:
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
g = pg.GraphItem()
Error displayed about GraphItem in PyCharm:
Cannot find reference 'GraphItem' in 'init.py'
The inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.
*I found what was causing me the trouble. It looks like scipy didn't install correctly although it didn't give me back an error, so I just had to install it in a different way.

Why import * and then ttk?

My understanding is that the standard set-up for a tkinter program starts off like this:
from tkinter import *
from tkinter import ttk
I understand that tkinter is a package, but if I've already imported all with the *, why do I still need to import ttk? Why do I get an error if I take out the second line and try to reference ttk?
When you do from some_package import *, python will import whatever that package chooses to export. What it chooses to export may be a subset of what is actually stored in the package folder. Why is that? There's no particular reason, it's just how the package author decided to do things.
This information about what to export is defined in the __init__.py file that is inside the package (in this case, tkinter/init.py). If you look at that file you'll notice that it doesn't import ttk itself, thus ttk won't be exported and therefore can't be imported with a wildcard import.
Again, there's no particular reason other than that's how the authors of tkinter and ttk chose to do things.
For more information on the mechanics of packaging, see the packaging portion of the python tutorial (https://docs.python.org/3/tutorial/modules.html#packages)
The better way to import tkinter
You may think it's standard because many tutorials do it that way, but it's generally a bad practice. The better way, IMO, is to give the tkinter library an explicit name:
# python 3.x
import tkinter as tk
from tkinter import ttk
# python 2.x
import Tkinter as tk
import ttk
This will make your code considerably easier to read, because you have to explicitly state which toolkit you are using:
b1 = tk.Button(...) # uses a standard tk button
b2 = ttk.Button(...) # uses a ttk button
I can think of no good reason to do it any other way. Doing a global import saves you a couple of bytes each time you call a tkinter function, but at the expense of clarity. Plus, it reinforces a bad practice that might bleed into how you use other libraries.
The real authority, IMO, is PEP8, which has this to say on the matter:
Wildcard imports (from import *) should be avoided as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).
Because tkinter/__init__.py doesn't import ttk, so ttk isn't included in from tkinter import *.
Briefly: from tkinter import * imports from file/packet tkinter but it doesn't mean that it will import from file/packet tkinter.ttk
what the other two answers here failed to state is that ttk is not imported because it is a submodule within the tkinter module, effectively a module in itself.
so when you import tkinter you get all of the parts that directly belong to tkinter
but ttk does not directly belong and so must be imported explicitly.
however Bryan Oakley makes a good point that importing everything from a module into local namespace (as many newbies do) can lead to big problems later on when you start to use more modules. this is because some of these modules may share function names even though the functions themselves may do completely different things.
it is always best for large modules to do:
import module as mod
or
import module
and then reference the functions as belonging to the modules namespace:
module.function()
this gives you more control over what you are doing and makes it clearer later on what the function actually belonged to.

Resources