By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Beyond "Self"

P: 15
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.

Expand|Select|Wrap|Line Numbers
  1. class textbox(pygame.sprite.Sprite):         # Textbox class
  2.  
  3.     def __init__(self):
  4.  
  5.         rect=*something*
  6.  
  7.     class textrender(pygame.sprite.Sprite): # Textrender class inside textbox class
  8.  
  9.         def __init__(self, text):
  10.  
  11.             #I want to set self.rect to textbox's rect
  12.  
Nov 23 '06 #1
Share this Question
Share on Google+
5 Replies


bartonc
Expert 5K+
P: 6,596
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?".
Nov 23 '06 #2

P: 15
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.Sprite 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.
Nov 23 '06 #3

bartonc
Expert 5K+
P: 6,596
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.Sprite 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
Nov 23 '06 #4

P: 2
I think he is looking something like Java inner classes, With Python is possible to do nested classes :
Expand|Select|Wrap|Line Numbers
  1. class Test(object):
  2.  
  3.     def __init__(self):
  4.     self.y = 0
  5.  
  6.     class TestInner(object):
  7.  
  8.     def __init__(self):
  9.         self.x = 0
  10.  
  11.     def someFunction(self):
  12.         print "Hello from Innerclass"
  13.  
  14. someObject = Test.TestInner()
  15.  
  16. someObject.someFunction()
  17.  
You can access innerclass methods and properties from the outer class but no the other way around.
Nov 30 '06 #5

bartonc
Expert 5K+
P: 6,596
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

Expand|Select|Wrap|Line Numbers
  1. class Test(object):
  2.  
  3.     def __init__(self):
  4.     self.y = 0
  5.  
  6.     class TestInner(object):
  7.  
  8.     def __init__(self):
  9.         self.x = 0
  10.  
  11.     def someFunction(self):
  12.         print "Hello from Innerclass"
  13.  
  14. someObject = Test.TestInner()
  15.  
  16. someObject.someFunction()
Nov 30 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.