473,399 Members | 3,401 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,399 software developers and data experts.

instances


Hello
I am trying to understand the abilities and limitation of creating an
instance. First I will give you my understanding then please steer me
in the right direction.

Abiities
1. The two ways to create an instance is def method(self) &
__int__(self, other, instances,...)
2. By creating an instance of a method; the functions of that method
can be used through out the
program in a fashion such as self.methodofprogram(parameters)
Limitations
3. One cannot create an instance of a class.
4. An instance can only perform functions that are provided from the
method it was instanced from.

5. Is there any other key information I am missing.
Jul 14 '06 #1
3 1642
Quenton Bonds wrote:
Hello
I am trying to understand the abilities and limitation of creating an
instance. First I will give you my understanding then please steer me
in the right direction.

Abiities
1. The two ways to create an instance is def method(self) &
__int__(self, other, instances,...)
2. By creating an instance of a method; the functions of that method
can be used through out the
program in a fashion such as self.methodofprogram(parameters)
Limitations
3. One cannot create an instance of a class.
4. An instance can only perform functions that are provided from the
method it was instanced from.

5. Is there any other key information I am missing.
Hi Quentin,
Might you be new to programming, or new to Object Oriented programming?
You might profit from reading some of the beginners tutorials mentioned
here:
http://wiki.python.org/moin/BeginnersGuide
which points to
http://wiki.python.org/moin/Beginner...NonProgrammers

Here is some info to correct your statements above (but it is not a
tutorial)!

The class statement creates a 'class definition'.
E.g:
class my_class():
pass
Instances of a class are created by using the class name followed by
parenthesis,
E.g:
my_inst = my_class()

If a class has methods:
class my_class2:
def my_method(self):
pass
Then an instance of the class:
my_inst2 = my_class2()
Can access its method like this:
my_inst2.my_method()

Trying to access a method of the class that does not exist will produce
an error.
E.g:
my_inst2.missing_method()
Will give an error, (throw an exception in Python terminology)..

The above is just a (poor) taste. Dive into one of the beginners
tutorials.

- Pad.

Jul 14 '06 #2
Quenton Bonds wrote:
Hello
I am trying to understand the abilities and limitation of creating an
instance. First I will give you my understanding then please steer me
in the right direction.
Wow, you've got it nearly completely comprehensively backwards.
Abiities
1. The two ways to create an instance is def method(self) &
__int__(self, other, instances,...)
There's really just basically one way to create an instance, and that's
by writing a class and then "calling" it. (example below)

if you use the def statement by itself, then you are creating a
FUNCTION object, that you later call with arguments to do some work.

When you create a class, it looks like this:

class foo:
def __init__(self, arg1, arg2):
# do some work to set up the instance of the class.

and then you "call" the class like so:

bar = foo(arg1, arg2)

to create an INSTANCE of the CLASS. the name 'bar' now references an
instance of class 'foo'.

(also note that when the def statement is used within a class, like the
__init__ above, then it creates a METHOD, which is almost the same
thing as a FUNCTION.)
2. By creating an instance of a method; the functions of that method
can be used through out the
program in a fashion such as self.methodofprogram(parameters)
Ok, I think the easiest thing to do here would be to rewrite your
sentence using the proper terminology:

By creating an instance of a CLASS, the METHODS of that CLASS can be
used through out the program in a fashion such as
INSTANCE.methodofCLASS(parameters)

Example of creating a class with a method:

class foo:
def __init__(self, arg1):
# do some work to set up the instance of the class.
self.value = arg1
def printme(self):
print self.value

Example of creating an instance of that class:

bar = foo('Hi there!')
Example of using a method of an instance of that class:

bar.printme()

# Prints "Hi there!"
Limitations
3. One cannot create an instance of a class.
:) One can ONLY create instances of classes.

4. An instance can only perform functions that are provided from the
method it was instanced from.
Yes, *IF* you replace "method" in that sentence with "class", and
"functions" with "methods".

5. Is there any other key information I am missing.
I hope this helps,
~Simon

Jul 14 '06 #3
Quenton,
What kind of instances do you want to create? An instance has to be an
instance of something. You mention creating instances "of a method",
what do you mean by that?

Anyway, assuming you are new to Python here is a basic intro about
objects and classes:
Think of a class as a blueprint and the objects are object built from
that blueprint. The object are called 'instances' of the class.
Creating an object of a specific class is called instantiating. So if
you have a blueprint for a car you use it to make a car. A car is an
instance instantiated from the car's blueprint.
Simple Example:
class Car:
def __init__(self,color):
self.color=color
def drive(self):
print "The",self.color,"car is driving"

That's a simple Car class i.e. the blueprint. It will make any car of
a specific color. To make an actual car object do:
jetta=Car("blue")
You have created an _instance_ of a class Car called jetta. Now,
according to the blueprint, you car will have one method you can call
and you call it like this::
jetta.drive()
and you should get the output "The blue car is driving".

Three things to notice:
1) 'self' is the first argument inside any method of the class. Both
__init__ and drive get it. Self references the object of the class
itself when those methods are called. For example the __init__ method
sets the color of the car. But it has to know where the car object is
so it can set its color. The car object itself is represented by 'self'
and then we set its color using self.color
2) the __init__ method is called a "magic" method. It can be called
_for you_ at a specific time. The __init__ method is used to initialize
a class object. So the object is created by Python then its __init__
method is called after that so you can customize it. Imagine that
Python makes a car for you and then it gives it to your __init__ method
for customization. Your __init__ method paints it a certain color and
you have your new car ready to go!
3) if you noticed from the semantics or form 2), __init__ does not
create an instance of a class by itself. It only customizes an already
created instance that it gets through the self argument. There is a
magic method called __new__ that Python uses for you to create an
instance of a class but with very very very rare exceptions you don't
even need to know about it (I don't even know what its parameters are,
because in all these years I have never had to use it).

Hope this helps, I assumed you are a new Python user that is why I
presented a simplistic example. Please see some Python tutorials and
documenation, you can search on Google for them.

Regards,
Nick Vatamaniuc


Quenton Bonds wrote:.
Hello
I am trying to understand the abilities and limitation of creating an
instance. First I will give you my understanding then please steer me
in the right direction.

Abiities
1. The two ways to create an instance is def method(self) &
__int__(self, other, instances,...)
2. By creating an instance of a method; the functions of that method
can be used through out the
program in a fashion such as self.methodofprogram(parameters)
Limitations
3. One cannot create an instance of a class.
4. An instance can only perform functions that are provided from the
method it was instanced from.

5. Is there any other key information I am missing.
Jul 14 '06 #4

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

Similar topics

22
by: christof hoeke | last post by:
hello, this must have come up before, so i am already sorry for asking but a quick googling did not give me any answer. i have a list from which i want a simpler list without the duplicates an...
4
by: Brad Tilley | last post by:
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:...
8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
4
by: Yasaswi Pulavarti | last post by:
I have a Aix 5.2 server with DB2 UDB 8.1 I created three different instances using the db2setup utility three times. The default instance is db2inst1. When I su - db2inst2 and su - db2inst3 and...
12
by: (Pete Cresswell) | last post by:
I know I can open many instances of a given form, but I've never done it. Now I'm analyzing an application where that seems like just the ticket: Many investment funds, *lots* of data points for...
4
by: Chad Myers | last post by:
I'm instrumenting my app with a few performance counters and I'd like to ask you all for some advice on how to handle performance counter instances. I have a class library that is a base library...
3
by: Boni | last post by:
Dear all, I create a big number of a class instances of some class. Sometimes this instances must be collected (all pointers are deleted, but instance is not explicitly disposed).But sometimes I...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
4
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
5
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.