Return to Snippet

Revision: 27721
at June 22, 2010 03:02 by Cesco


Updated Code
class DisneyCharacter():
 
    # This variable holds the name for the class DisneyCharacter
    name = ""
 
    class Duck():
 
        # This variable holds the name for the class Duck
        name = ""
 
        def __init__(self, name = None):
            """ Constructor for the class Duck """
            # If the user doesn't specify a second name, "Duck" will be chosen
            if (name is None): name = "Duck"
            self.name = name
 
        def getName(self):
            """ Returns the name stored in the class Duck """
            return self.name
 
    def __init__(self, name = None, secondName = None):
        """ Constructor for the class DisneyCharacter """
        # If the user doesn't specify a name, "Donald" will be chosen
        if (name is None): name = "Donald"        
        # Creates a new instance of Duck
        newDuck = DisneyCharacter.Duck(secondName)
        # This will join together the names from the classes DisneyCharacter and Duck
        self.name = name + " " + newDuck.getName()
 
    def getName(self):
          """ Returns the name stored in the class DisneyCharacter """
          return self.name
 
# Creates an instance of the class DisneyCharacter
donald = DisneyCharacter()
 
# Writes "Donald Duck"
print(donald.getName())
 
# Writes "Daisy Duck"
daisy = DisneyCharacter("Daisy")
print(daisy.getName())

# Writes "Scrooge McDuck"
unclescrooge = DisneyCharacter("Scrooge", "McDuck")
print(unclescrooge.getName())

Revision: 27720
at June 22, 2010 03:02 by Cesco


Updated Code
class DisneyCharacter():
 
    # This variable holds the name for the class DisneyCharacter
    name = ""
 
    class Duck():
 
        # This variable holds the name for the class Duck
        name = ""
 
        def __init__(self, name = None):
            """ Constructor for the class Duck """
            # If the user doesn't specify a second name, "Duck" will be chosen
            if (name is None): name = "Duck"
            self.name = name
 
        def getName(self):
            """ Returns the name stored in the class Duck """
            return self.name
 
    def __init__(self, name = None, secondName = None):
        """ Constructor for the class DisneyCharacter """
        # If the user doesn't specify a name, "Donald" will be chosen
        if (name is None): name = "Donald"        
        # Creates a new instance of Duck
        newDuck = DisneyCharacter.Duck(secondName)
        # This will join together the names from the classes DisneyCharacter and Duck
        self.name = name + " " + newDuck.getName()
 
    def getName(self):
          """ Returns the name stored in the class DisneyCharacter """
          return self.name
 
# Creates an instance of the class DisneyCharacter
donald = DisneyCharacter()
 
# Writes "Donald Duck"
print(donald.getName())
 
# Writes "Daisy Duck"
daisy = DisneyCharacter("Daisy")
print(daisy.getName())

# Writes "Scrooge McDuck"
gladstone = DisneyCharacter("Scrooge", "McDuck")
print(gladstone.getName())

Revision: 27719
at June 22, 2010 02:49 by Cesco


Updated Code
class DisneyCharacter():

    # This variable holds the name for the class DisneyCharacter
    name = ""
    
    class Duck():
        
        # This variable holds the name for the class Duck
        name = ""
       
        def __init__(self):
            """ Constructor for the class Duck """
            self.name = "Duck"
       
        def getName(self):
            """ Returns the name stored in the class Duck """
            return self.name
   
    def __init__(self, name = None):
        """ Constructor for the class DisneyCharacter """
        # If the user doesn't specify a name, "Donald" will be chosen
        if (name is None): name = "Donald"
        # Creates a new instance of Duck
        newDuck = DisneyCharacter.Duck()
        # This will join together the names from the classes DisneyCharacter and Duck
        self.name = name + " " + newDuck.getName()
          
    def getName(self):
          """ Returns the name stored in the class DisneyCharacter """
          return self.name

# Creates an instance of the class DisneyCharacter
donald = DisneyCharacter()

# Writes "Donald Duck"
print(donald.getName())

# Writes "Daisy Duck"
daisy = DisneyCharacter("Daisy")
print(daisy.getName())

Revision: 27718
at June 22, 2010 02:37 by Cesco


Updated Code
class DisneyCharacter():

    # This variable holds the name for the class DisneyCharacter
    name = ""
    
    class Duck():
        
        # This variable holds the name for the class Duck
        name = ""
       
        def __init__(self):
            """ Constructor for the class Duck """
            self.name = "Duck"
       
        def getName(self):
            """ Returns the name stored in the class Duck """
            return self.name
   
    def __init__(self, name = None):
        """ Constructor for the class DisneyCharacter """
        # If the user doesn't specify a name, "Donald" will be chosen
        if (name is None): name = "Donald"
        # Creates a new instance of duck
        newDuck = DisneyCharacter.Duck()
        # This will join together the names from the classes DisneyCharacter and Duck
        self.name = name + " " + newDuck.getName()
          
    def getName(self):
          """ Returns the name stored in the class DisneyCharacter """
          return self.name

# Creates an instance of the class DisneyCharacter
donald = DisneyCharacter()

# Writes "Donald Duck"
print(donald.getName())

# Writes "Daisy Duck"
daisy = DisneyCharacter("Daisy")
print(daisy.getName());

Revision: 27717
at June 22, 2010 02:35 by Cesco


Updated Code
class DisneyCharacter():

    # This variable holds the name for the class DisneyCharacter
    name = ""
    
    class Duck():
        
        # This variable holds the name for the class Duck
        name = ""
       
        def __init__(self):
            """ Constructor for the class Duck """
            self.name = "Duck"
       
        def getName(self):
            """ Returns the name stored in the class Duck """
            return self.name
   
    def __init__(self, name = None):
        """ Constructor for the class DisneyCharacter """
        # If the user doesn't specify a name, "Donald" will be chosen
        if (name is None): name = "Donald"
        newDuck = DisneyCharacter.Duck()
        self.name = name + " " + newDuck.getName()
          
    def getName(self):
          """ Returns the name stored in the class Character """
          return self.name

# Creates an instance of the class DisneyCharacter
donald = DisneyCharacter()

# Writes "Donald Duck"
print(donald.getName())

# Writes "Daisy Duck"
daisy = DisneyCharacter("Daisy")
print(daisy.getName());

Revision: 27716
at June 22, 2010 02:33 by Cesco


Initial Code
class DisneyCharacter():

    # This variable holds the name for the class DisneyCharacter
    name = ""
    
    class Duck():
        
        # This variable holds the name for the class Duck
        name = ""
       
        def __init__(self):
            """ Constructor for the class Duck """
            self.name = "Duck"
       
        def getName(self):
            """ Returns the name stored in the class Duck """
            return self.name
   
    def __init__(self, name = None):
        """ Constructor for the class DisneyCharacter """
        # If the user doesn't specify a name, "Donald" will be chosen
        if (name is None): name = "Donald"
        newDuck = DisneyCharacter.Duck()
        self.name = name + " " + newDuck.getName()
          
    def getName(self):
          """ Returns the name stored in the class Character """
          return self.name

# Creates an instance of the class Donald
instance = DisneyCharacter()

# Writes "Donald Duck"
print(instance.getName())

# Writes "Daisy Duck"
daisy = DisneyCharacter("Daisy")
print(daisy.getName());

Initial URL

                                

Initial Description

                                

Initial Title
Nested classes: How to put a class inside another in Python

Initial Tags
class

Initial Language
Python