Beyond "Self"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spacecoyote
    New Member
    • Nov 2006
    • 15

    Beyond "Self"

    Hello,

    I have a class inside a class. I want to set a variable "rect" in that class "textrender " to the value of a variable "rect" in the parent class "textbox". I don't know how to do this.

    Code:
    class textbox(pygame.sprite.Sprite): 		# Textbox class
    			
    	def __init__(self):
    			
    		rect=*something*
    				
    	class textrender(pygame.sprite.Sprite): # Textrender class inside textbox class
    				
    		def __init__(self, text):
    			
    			#I want to set self.rect to textbox's rect
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Well, spacecoyote,
    I've been writing classes for a couple of years and have read my share of examples, but I've never seen a class inside a class before. What you want to do is probably possible; my question is "are you sure that's what you want to do?".

    Comment

    • spacecoyote
      New Member
      • Nov 2006
      • 15

      #3
      Well, I've found another more useful way to do it.

      But there are cases where it is necessary to have a class inside a class.

      In pygame, for example, to make a sprite you make a class which inherits pygame.sprite.S prite and then overload whatever you need to (ie __init__).

      Then you have your functions which are inside a class (becuase programs are separated into subprograms which are separted into functions, etc, etc. So when you use a sprite in a function, its a class in a class.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by spacecoyote
        Well, I've found another more useful way to do it.

        But there are cases where it is necessary to have a class inside a class.

        In pygame, for example, to make a sprite you make a class which inherits pygame.sprite.S prite and then overload whatever you need to (ie __init__).

        Then you have your functions which are inside a class (becuase programs are separated into subprograms which are separted into functions, etc, etc. So when you use a sprite in a function, its a class in a class.
        OK. Perhaps I've just never seen this. I know that it is valid in the language. Did you see my post class instance in a class

        Comment

        • alpha
          New Member
          • Nov 2006
          • 2

          #5
          I think he is looking something like Java inner classes, With Python is possible to do nested classes :
          Code:
          class Test(object):
          
              def __init__(self):
          	self.y = 0
          
              class TestInner(object):
          
          	def __init__(self):
          	    self.x = 0
          
          	def someFunction(self):
          	    print "Hello from Innerclass"
          
          someObject = Test.TestInner()
          
          someObject.someFunction()
          You can access innerclass methods and properties from the outer class but no the other way around.
          Last edited by bartonc; Feb 23 '07, 06:23 AM. Reason: added [code][/code] tags

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by alpha
            I think he is looking something like Java inner classes, With Python is possible to do nested classes :

            You can access innerclass methods and properties from the outer class but no the other way around.
            But noone can see your structure 'til you learn to use code tags (see sticky at the top of the forum or panel on the right while you are posting called "posting guidelines). thanks

            Code:
            class Test(object):
            
                def __init__(self):
            	self.y = 0
            
                class TestInner(object):
            
            	def __init__(self):
            	    self.x = 0
            
            	def someFunction(self):
            	    print "Hello from Innerclass"
            
            someObject = Test.TestInner()
            
            someObject.someFunction()

            Comment

            Working...