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. -
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
-
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?".
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.
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
I think he is looking something like Java inner classes, With Python is possible to do nested classes : -
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.
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 - 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()
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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.
|
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__:
|
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.
|
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
| |
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)
|
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)
|
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
|
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,...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
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...
| |