These three things are holding me back.

1. The new style class object parameter -

class MyClass(object)

Note that I'M also also not familir with the old style

2. The

_init_(

- constructor, or any constructs for that matter.

3. The

(self)

parameter. How is it different from the normal parameters or arguments?

Recommended Answers

All 2 Replies

Here is class example have i tryed to explain som basic thing about the making of a class and how thing work.

class SimpleClass(object):
  '''
  Doc string info about class  
  * SimpleClass inherits the most basic container class object (just a place holder)
  * this is the newer class writing convention, adding (object) is "still" optional  
  '''  
  # Class attributes as you see this is variables
  # They the belong to a class therefor the name 'attributes' 
  x = 5
  y = 10
  s = x + y
  
  def __init__(self, string):    
     '''
     Self is the class glue,it`s a carrier off data between metods.
     The variable s is assigned a value in the class, but outside of the methods.
     You can access s in a method using self.s #****    
     When you make_objekt everything in this method get executed because of __init__
     __init__ acts as Constructor when you make_object everything under __init__ method get executed
     You dont need to call like next method SayHello
     '''
     self.string = string
     print 'In the constructor'
     print 'Value from argument -->', string
     print 'This is my lucky number %s' % self.s #*****
     print ''     
      
  def SayHello(self, string):
    '''This look like a function,but in a class it is called a method'''
    print 'Hi, ' + string + '.'
    
  def data_get(self):
    '''Recive data from __init__'''
    print 'Data from __init__ -->', self.string  #Try without self(remember carrier of data)      
   

# Run 1 and 1 line try to understand whats happens    
make_object = SimpleClass('hello')  #Class instance(pay attention that __init__ method get executed when you run it now)
#make_object_new = SimpleClass("argument from new object")  #New class instance 

#make_object.SayHello('This is Python and i like it')   #Call method SayHello()  
#make_object_new.SayHello('Object_new like python to')  #Call method SayHello() from new object

#make_object.data_get()     #Call method data_get()

#print make_object.x        #Call attribute 
#print make_object.s        #Call attribute 
#print make_object_new.y    #Call attribute from new object

#print make_object.__doc__  #Print doc string
#print help(SimpleClass)    #Print help for this class

Don't worry about adding (object) right now. It is only needed in special cases, and that need is gone in Python3 anyway. Here is an example class that should explain some of your questions ...

class MyClass:
    def __init__(self, animal, sound):
        """
        The constructor __init__() brings in external
        parameters when an instance of the class is created,
        self keeps track of the specific instance and makes
        instance variables global to the class so the class
        methods can use them
        """
        self.animal = animal
        self.sound = sound
        
    def animal_sound(self):
        """
        a class method has self for the first argument
        note that self.animal and self.sound are global 
        to the class and specific to the instance
        """
        print( "The %s goes %s" % (self.animal, self.sound) )

# create an instance of the class
# you have to supply the animal and it's sound
# as shown in the class __init__()
cow = MyClass('cow', 'mooh')

# create another instance
dog = MyClass('dog', 'woof')

# now let's do something with the instance
cow.animal_sound()  # The cow goes mooh

dog.animal_sound()  # The dog goes woof
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.