473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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(ideally 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\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py" ,
line 301, in RunScript
exec codeObject in __main__.__dict__
File "X:\ibutt_1\EEU\development\prototype\martini\scri pts\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 1722
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(test) # this is crucial

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

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

def _privateByConvention(self):
pass

and then call it with a class or instance:

Sample.test("from class")
instance = Sample()
instance.test("from instance")
instance.indirect("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******@rogers.com> schrieb im Newsbeitrag
news:37**************************@posting.google.c om...


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
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...
5
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...
7
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...
7
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...
22
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...
0
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...
2
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...
0
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.