473,563 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

instances v. threads

I've just started using classes in Python for some projects at work and
have a few questions about them. I understand that once a class is
defined that I can create many instances of it like this:

class xyz:
def one():
pass
def two ():
pass
def three():
pass

a = xyz()
b = xyz()
c = xyz()

a.one()
b.one()
c.one()
c.two()
c.three()

How does asynchronous/threaded programming differ from OO programming
and classes? C is not OO and has no classes, but one can write threaded
programs in C, right? Perhaps I'm totally off on this... can some
explain how these concepts differ? Exactly how is an 'instance'
different from a 'thread'?

Thanks,
Brad
Jul 18 '05 #1
4 1315
Brad Tilley wrote:
I've just started using classes in Python for some projects at work and
have a few questions about them. I understand that once a class is
defined that I can create many instances of it like this:

class xyz:
def one():
pass
def two ():
pass
def three():
pass

a = xyz()
b = xyz()
c = xyz()

a.one()
b.one()
c.one()
c.two()
c.three()

How does asynchronous/threaded programming differ from OO programming
and classes? C is not OO and has no classes, but one can write threaded
programs in C, right? Perhaps I'm totally off on this... can some
explain how these concepts differ? Exactly how is an 'instance'
different from a 'thread'?

Thanks,
Brad
Heya Brad -

Looks like you're confusing two unrelated things.

I'm not sure what you know about either thing, so forgive me if I am too
basic in my attempt at explaining.

Take two simple Python statements:

a = "123"
b = "456"

What have I? A variable named a that has the str data "123" in it, and one
named B that has the str data "456" in it. Two variables (or "objects") set
aside in memory, each storing a different value. You'd agree that the
second line that sets the variable "b" does *not* create a new thread, or
unit of execution, right?

Of course not. If this was my whole program, I'd have, effectively, one
thread - the process itself, and two variables.

Think of "instances" the same exact way. In your example above, the lines
a = xyz()
b = xyz()
c = xyz()
create three new variables, each assigned to a new copy of your class xyz.
When you call a method in xyz, such as a.one(), you are *not* creating a
new thread. The program calls this method just as with any function, and
when the function or method is done, it returns to your mainline code.

In other words, in your code example,
a.one()
b.one()
c.one()
c.two()
c.three()


each of these executes *in turn*, not simultaneously.

Now, suppose your class really looked like this:

class xyz:
def one(stuff):
self.data = stuff

and I did

a = xyz()
b = xyz()
c = xyz()

then followed it up with

a.one("Hello, I am instance A!")

What is the value of a.data? "Hello, I am instance A!", of course. And the
value of b.data and c.data? Not yet defined.

In my example, I have three instances of class xyz, and each has it's own
private data. But that's it - each one does *not* create it's own thread;
they do not execute asynchronously.

If you're used to C programming, imagine classes as being similar to
structs, just with the ability to include functions as well as data.

Along the same lines, if I wanted to have multiple threads active, I'd have
to create them, using any language that has a threading library (C does,
Python does, Java does, etc.). I can certainly use classes and instances in
my threads, but the two are not the similar concepts.

Make any sense?

Rob
Jul 18 '05 #2
Rob Snyder wrote:
....
Now, suppose your class really looked like this:

class xyz:
def one(stuff):
self.data = stuff

Let's make this (as I'm sure you know): class xyz:
def one(self, stuff):
self.data = stuff


I just want to make sure newbies reading this doesn't get confused.

--Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #3
Scott David Daniels wrote:
Rob Snyder wrote:
...
Now, suppose your class really looked like this:

class xyz:
def one(stuff):
self.data = stuff


Let's make this (as I'm sure you know):
> class xyz:
> def one(self, stuff):
> self.data = stuff


I just want to make sure newbies reading this doesn't get confused.

--Scott David Daniels
Sc***********@A cm.Org


<smacks head> thanks Scott!
Jul 18 '05 #4
Rob Snyder wrote:
Brad Tilley wrote:

I've just started using classes in Python for some projects at work and
have a few questions about them. I understand that once a class is
defined that I can create many instances of it like this:

class xyz:
def one():
pass
def two ():
pass
def three():
pass

a = xyz()
b = xyz()
c = xyz()

a.one()
b.one()
c.one()
c.two()
c.three()

How does asynchronous/threaded programming differ from OO programming
and classes? C is not OO and has no classes, but one can write threaded
programs in C, right? Perhaps I'm totally off on this... can some
explain how these concepts differ? Exactly how is an 'instance'
different from a 'thread'?

Thanks,
Brad

Heya Brad -

Looks like you're confusing two unrelated things.

I'm not sure what you know about either thing, so forgive me if I am too
basic in my attempt at explaining.

Take two simple Python statements:

a = "123"
b = "456"

What have I? A variable named a that has the str data "123" in it, and one
named B that has the str data "456" in it. Two variables (or "objects") set
aside in memory, each storing a different value. You'd agree that the
second line that sets the variable "b" does *not* create a new thread, or
unit of execution, right?

Of course not. If this was my whole program, I'd have, effectively, one
thread - the process itself, and two variables.

Think of "instances" the same exact way. In your example above, the lines

a = xyz()
b = xyz()
c = xyz()

create three new variables, each assigned to a new copy of your class xyz.
When you call a method in xyz, such as a.one(), you are *not* creating a
new thread. The program calls this method just as with any function, and
when the function or method is done, it returns to your mainline code.

In other words, in your code example,

a.one()
b.one()
c.one()
c.two()
c.three()

each of these executes *in turn*, not simultaneously.

Now, suppose your class really looked like this:

class xyz:
def one(stuff):
self.data = stuff

and I did

a = xyz()
b = xyz()
c = xyz()

then followed it up with

a.one("Hello, I am instance A!")

What is the value of a.data? "Hello, I am instance A!", of course. And the
value of b.data and c.data? Not yet defined.

In my example, I have three instances of class xyz, and each has it's own
private data. But that's it - each one does *not* create it's own thread;
they do not execute asynchronously.

If you're used to C programming, imagine classes as being similar to
structs, just with the ability to include functions as well as data.

Along the same lines, if I wanted to have multiple threads active, I'd have
to create them, using any language that has a threading library (C does,
Python does, Java does, etc.). I can certainly use classes and instances in
my threads, but the two are not the similar concepts.

Make any sense?

Rob


Thanks Rob, that makes a lot of sense. I thought that instances ran
simultaneously, not sequentially. Your explanation was right on... I
understand how to think about instances now.
Jul 18 '05 #5

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

Similar topics

6
907
by: Angelos Karantzalis | last post by:
Hi y'all ... I'm a bit puzzled here about .NET class instancing under COM+ Issue 1: I've a COM+ component, let's call it ... COMDbWrapper that initializes itself from an xml file. The data in the file change very rarely, so I would like to keep it in a single copy in-memory if that's possible.
6
2389
by: ouech | last post by:
hi, I'd like to know if i need mutexs to lock the use of member methods of a class instance shared between several threads via a pointer? if the method modify member variables, i know that mutexs are needed but the case that interrest me is when the method never modify a member variable. I really have no idea cause i don't know if the...
4
2041
by: Roland Riess | last post by:
Hi all, at the moment i am developing an app which is sort of an interface to copy data from one database to another and it shall run as a service. As there are several databases the app must be installed and run in multiple instances with the configuration data (database, login, etc.) stored in an app.config file. Up to now I was...
2
2090
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these instances to process each incoming HTTP request that is received by the application. The particular HttpApplication instance assigned is responsible for...
2
2116
by: Markus Prediger | last post by:
Hi NG, I have an asp.net project that uses an vb6 com object for some database-manipulation (I cannot rewrite it in .net, sorry, its not my decision). I want it to be instanciated seperately for each session, so that three users can connect to three different databases. But I get crazy because: -With interop all users share one...
10
4963
by: John | last post by:
I currently have a Windows Service that runs Transactions that are very Processor/Memory Intensive. I have a requirement to deploy multiple instances of the Web service on the Same server. Each Instance needs to run in its own process. My current approach to this is to put all the logic into a separate "Worker" assembly and install it into...
19
6315
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they can all access. It seems like a needless waste of memory to make them all maintain their own copy of the same data in RAM at the same time. ...
10
5510
by: Terry | last post by:
Hi, Is there a simple way to get all the instances of one class? I mean without any additional change to the class. br, Terry
5
1725
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the members Y is the location of a file to be read. The original design assumes that this class will be instantiated and each instance will happily mange...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.