473,383 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

gobal var inside class without notice???

I have code like this:

class A:
def __init__(self,j):
self.j = j

def something(self):
print self.j
print i # PROBLEM is here there is no var i in class A but it
works ???

if __name__ == '__main__':
i = 10
a = A(5)
a.something()

I don't define global i but it will takes var i from outside of class
A.

Can somebody explain this ???

pujo

Jul 19 '05 #1
2 1191
aj****@gmail.com wrote:
I have code like this:

class A:
def __init__(self,j):
self.j = j

def something(self):
print self.j
print i # PROBLEM is here there is no var i in class A but it
works ???

if __name__ == '__main__':
i = 10
a = A(5)
a.something()

I don't define global i but it will takes var i from outside of class
A.

Can somebody explain this ???

pujo

Actually you *have* defined a global variable called 'i' - when you run
code at file scope, as you have where you wrote i = 10, all variables
are global. This means that they can be referred to anywhere in the
module, as you found.

It also means that variable accesses are slower, since accessing globals
is slower than accessing local variables. This is why many people
suggest that you define a function called main() and have the main
script simply call that.

Michael
Jul 19 '05 #2
Thanks a lot.
pujo

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Tom wilson | last post by:
I'm trying to create dynamic controls in ASP.Net. It's driving me nuts. I keep getting the error: Control '16' of type 'RadioButton' must be placed inside a form tag with runat=server. Dim...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
3
by: Wilson | last post by:
Hi, How can I create a global and session dataset using in the web applicaton ? I have defined in Global class without luck. e.g. public DataSet ds_applicaton; public DataSet ds_session;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.