473,796 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a utility class and having a method call another method from within

Ian
I have written this utility class that has a method which calls
another method. I don't want to pass in the self parameter and was
wondering by doing this does it make the function to be private or
uncallable outside of the class.

This code below works, but I didn't really want to pass self into
testBob1(ideall y having it as private), I wanted the declaration to be
def testBob1(a). Can someone explains to me why this can't be done ?
I would get the following error:

Traceback (most recent call last):
File "C:\Python22\Li b\site-packages\Python win\pywin\frame work\scriptutil s.py",
line 301, in RunScript
exec codeObject in __main__.__dict __
File "X:\ibutt_1\EEU \development\pr ototype\martini \scripts\aaa.py ",
line 3, in ?
print SampleClassd(). testBob2(2)
File "SampleClass.py ", line 15, in testBob2
return SampleClassd(). testBob1(a)
TypeError: testBob1() takes exactly 1 argument (2 given
in file-->SampleClass. py

class SampleClassd :

def __init__(self) :
self.test = 'a'

def testBob1(self, a) :
if a == 1:
return True
else:
return False

def testBob2(self, a) :
return SampleClassd(). testBob1(a)

Main file
=========

from SampleClass import SampleClassd

print SampleClassd(). testBob2(2)
Jul 18 '05 #1
2 1738
Ian wrote:
I have written this utility class that has a method which calls
another method. I don't want to pass in the self parameter and was
wondering by doing this does it make the function to be private or
uncallable outside of the class.


For new style classes, you can write methods without a self parameter:

class Sample(object):
def test(arg):
print arg
test = staticmethod(te st) # this is crucial

def indirect(self, arg):
Sample.test("in direct(%s)" % arg)

def __mangled(self) :
# transformed into _Sample__mangle d by the compiler
pass

def _privateByConve ntion(self):
pass

and then call it with a class or instance:

Sample.test("fr om class")
instance = Sample()
instance.test(" from instance")
instance.indire ct("from instance")

In Python, privacy is rather a gentlemen's agreement. You can start a method
name with a single underscore "_" to signal "use at your own risk".
Two underscores trigger some name mangling by the compiler and can be used
to avoid name clashes. Occasions where this is useful, e. g. to protect a
method against overriding in a subclass, should be rare.

Peter

Jul 18 '05 #2

"Ian" <2i******@roger s.com> schrieb im Newsbeitrag
news:37******** *************** ***@posting.goo gle.com...


in file-->SampleClass. py

class SampleClassd :

def __init__(self) :
self.test = 'a'

def testBob1(self, a) :
if a == 1:
return True
else:
return False

def testBob2(self, a) :
return SampleClassd(). testBob1(a)


How about:

def testBob2(self,a ):
return self.testBob1(a )

HTH

Cu Ulrich
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1737
by: mwnorris | last post by:
Thank you for your help in advance! I am having a serious difficulty with a database conversion utility that I have created using Director and Datagrip to help support a database application that we already have in the field. We just updated our database structure and this utility reads information from the previous version database and plugs it into the new database.
5
1882
by: Neil Zanella | last post by:
Hello, I need to access some user defined utility functions from within my ASP.NET pages. I wonder whether there is a way to do this. I do not want to use inheritance. I just want to be able to call some code contained in a .cs file (C# file) from within several ..aspx and .ascx page without having to rewrite the code in each such page. I would like to know how this can be accomplished, including how I can ensure that ASP.NET will find...
7
2197
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from another. I have tried declaring them as shared, public, friend, etc and I always get an error stating that something is not valid on a local variable declaration. For example, in the following code for Sub DataGrid_Select, I have CurrentID and...
7
2583
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be stupid or missing the point. I used DrawString( ) as a simple test but I cannot get it to work at all unless I handle my custom controls Paint event.
22
5358
by: John Salerno | last post by:
I might be missing something obvious here, but I decided to experiment with writing a program that involves a class, so I'm somewhat new to this in Python. Anyway, what is the best way to create a function (A) within a class that another function (B) can use? Function A is not something that an instance will ever call, so I figure it's a choice between static or class methods, but I don't know which one, or if this is even the right...
0
5608
by: info | last post by:
Dear all, is the first time that I use SOAP, and i must say that i'm having several problems. this is SOAP message that expects the server =================XML EXPECTED FROM THE SERVER================ <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
2
2582
by: Ken Crismon | last post by:
Hello, I am currently working on an embedded systems project where by I have written a small web server that is being hosted on our internet appliance (running on an Atmega128 chip and doing lots of col RFID and Mesh network stuff). In this example I am trying to get the AuthenticateUser web service to work. I am using the following WSDL:
0
5577
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
2
10049
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
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...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
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,...
1
7553
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
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
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
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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 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.