importing without executing the class - python

2024/10/11 12:23:46

my problem is about i have a file that contain class and inside this class there is bunch of code will be executed

so whenever i import that file it will executed ! without creating an object of the class ! , here is the example

FILE X

class d:def __init__(self):print 'print this will NOT be printed'print "this will be printed"

file B

import x

output is this will be printed, so my question is how to skip executing it until creating a new object?

Answer

You can't do that in Python, in Python every class is a first level object, a Python class is an object too and an class attribute can exist even if there is no instances of that class. If you just want to suppress the output of the print statement you can redirect the output of your print statements on the importing moment or create a context like the one provided in this first answer and use the __import__ statement manually.

https://en.xdnf.cn/q/69771.html

Related Q&A

If a command line program is unsure of stdouts encoding, what encoding should it output?

I have a command line program written in Python, and when I pipe it through another program on the command line, sys.stdout.encoding is None. This makes sense, I suppose -- the output could be another…

How to generate JSON-API data attribute vs results attribute in Django Rest Framework JSON API?

I have a django 1.9.2 project using Django Rest Framework JSON API:https://github.com/django-json-api/django-rest-framework-json-api:My viewset looks like this:class QuestionViewSet(viewsets.ReadOnlyMo…

How connect my GoPro Hero 4 camera live stream to openCV using Python?

I m having troubles trying to capture a live stream from my new GoPro Hero 4 camera and do some image processing on it using openCV.Here is my trial (nothing shows up on the created windowimport cv2 im…

I thought Python passed everything by reference?

Take the following code#module functions.py def foo(input, new_val):input = new_val#module main.py input = 5 functions.foo(input, 10)print inputI thought input would now be 10. Why is this not the cas…

Python: -mno -cygwin

im trying to learn a lot of python on windows and that includes installing several packages, however everytime i invoke python setup.py install i have a problem with -mno -cygwin for gcc. ive have rea…

Django Sites Framework initial setup

Im comfortable with fairly one-dimensional Django implementations, but now trying to understand the multi-sites-with-shared-stuff process. Ive read through the Django Sites Framework and many posts o…

Data corruption: Wheres the bug‽

Last edit: Ive figured out what the problem was (see my own answer below) but I cannot mark the question as answered, it would seem. If someone can answer the questions I have in my answer below, name…

Python NetworkX — set node color automatically based on a list of values

I generated a graph with networkx import networkx as nx s = 5 G = nx.grid_graph(dim=[s,s]) nodes = list(G.nodes) edges = list(G.edges) p = [] for i in range(0, s):for j in range(0, s):p.append([i,j])…

control wspace for matplotlib subplots

I was wondering: I have a 1 row, 4 column plot. However, the first three subplots share the same yaxes extent (i.e. they have the same range and represent the same thing). The forth does not. What I w…

Getting indices of both zero and nonzero elements in array

I need to find the indicies of both the zero and nonzero elements of an array.Put another way, I want to find the complementary indices from numpy.nonzero().The way that I know to do this is as follows…