473,769 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beyond "Self"

15 New Member
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
5 1688
bartonc
6,596 Recognized Expert Expert
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
spacecoyote
15 New Member
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.
Nov 23 '06 #3
bartonc
6,596 Recognized Expert Expert
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
Nov 23 '06 #4
alpha
2 New Member
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
6,596 Recognized Expert Expert
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

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

Similar topics

3
2430
by: Todd Gardner | last post by:
Pardon my extremely ignorant newbie questions. Where can I go to find more information about the "self" argument? Is there something special about the word "self" or did Mr. Guido van Rossum just decide to us the word arbitrarily? More precisely it seems that most all classes have "self" as the first parameter. This may be directly obvious but is the word "self" always referring to the class or the function it is used in?
1
1635
by: Olaf Meding | last post by:
What does the below PyChecker warning mean? More importantly, is there a way to suppress it? PyChecker warning: ..\src\phaseid\integration.py:21: self is argument in staticmethod My best guess is that the warning is related to PyChecker not supporting C++ extensions.
5
2833
by: BJörn Lindqvist | last post by:
I think it would be cool if you could refer to instance variables without prefixing with "self." I know noone else thinks like me so Python will never be changed, but maybe you can already do it with Python today? ..import sys .. ..def magic(): .. s = "" .. for var in sys._getframe(1).f_locals.__dict__:
26
2172
by: Fernando M. | last post by:
Hi, i was just wondering about the need to put "self" as the first parameter in every method a class has because, if it's always needed, why the obligation to write it? couldn't it be implicit? Or is it a special reason for this being this way? Thanks.
20
2867
by: Wayne Sutton | last post by:
OK, I'm a newbie... I'm trying to learn Python & have had fun with it so far. But I'm having trouble following the many code examples with the object "self." Can someone explain this usage in plain english? Thanks, Wayne
9
2777
by: shannonl | last post by:
Hi all, For some reason this bind is calling the donothing function, like it should, but is then allowing the text to be inserted into the Text widget. Here is the code: self.framebody.tag_config("name", underline=1) self.framebody.tag_bind("name", "<Any-KeyPress>", self.donothing)
13
12020
by: Kurda Yon | last post by:
Hi, I found one example which defines the addition of two vectors as a method of a class. It looks like that: class Vector: def __add__(self, other): data = for j in range(len(self.data)): data.append(self.data + other.data)
8
2704
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am always doing why cant this be automated or abstracted away? Are the instances where I won't pass self? I imagine there is some tradeoff involved otherwise it would have been
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10208
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7404
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.