473,382 Members | 1,329 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,382 software developers and data experts.

calling a class instance of function


class pid:
"pid"
def add(original_pid,toadd):
"add pid"
original_pid.append(toadd)
return original_pid
def remove(something_to_remove_from,what):
"remove pid"
something_to_remove_from.remove(what)
return something_to_remove_from
def modify(source,element,changewiththis):
"modify pid"
source[source.index(element)]=changewiththis
return source

class test(pid):
pid.original=[1,2,3]
pid.toadd=4
pid.add(pid.original,pid.toadd) # error here says that
# it expects pid instance as first arg
# not a list that it got.

why do i get an error?
Dec 21 '06 #1
4 1390
Pyenos wrote:
class test(pid):
pid.original=[1,2,3]
pid.toadd=4
pid.add(pid.original,pid.toadd) # error here says that
# it expects pid instance as first arg
# not a list that it got.

why do i get an error?
'test' is a class here, and the pid you tried to pass it is actually
the class it is inheriting from. I think you wanted to do:

def test(pid):
...

Also, in order to call a function without arguments you still need to
use (), so you probably wanted to use pid.original() in your pid.add
call.

Brett Hoerner

Dec 21 '06 #2
Brett Hoerner wrote:
Also, in order to call a function without arguments you still need to
use (), so you probably wanted to use pid.original() in your pid.add
call.
Sorry, never mind this bit, I misread the line. But you do probably
want to change the class test into a function test.

Brett Hoerner

Dec 21 '06 #3
On 20 dic, 21:09, Pyenos <pye...@pyenos.orgwrote:
class pid:
"pid"
def add(original_pid,toadd):
"add pid"
original_pid.append(toadd)
return original_pid
For a method, you have to include an *explicit* first parameter
(usually called "self") that refers to the instance on which you call
the method (like "this" on other languages, but you have to be explicit
in Python).
Read the Python tutorial: http://docs.python.org/tut/

--
Gabriel Genellina

Dec 21 '06 #4
Hi,

Methods i.e functions bound to a class instance (or object) the self argument in their definition:

Expand|Select|Wrap|Line Numbers
  1. class pid:
  2. def add(self, toadd):
  3. pass #or some sensible code
  4.  
If you want to define a method without that implicit self argument you'll have to make this method a static:

Expand|Select|Wrap|Line Numbers
  1. class pid:
  2. @staticmethod
  3. def staticadd (param1, param2):
  4. pass #or some sensible code
  5.  
Furthermore, your test() seems to be rather a function than a class:
You want to use this function to test the pid class but what you do with your code below is to define a class test which inherits pid. I'm not really sure what the following lines do ... this would usually be the place to introduce class variables.

Hope that Helps!

Greetings

Nils

-------- Original-Nachricht --------
Datum: 21 Dec 2006 11:09:32 +1100
Von: Pyenos <py****@pyenos.org>
An: py*********@python.org
Betreff: calling a class instance of function
>
class pid:
"pid"
def add(original_pid,toadd):
"add pid"
original_pid.append(toadd)
return original_pid
def remove(something_to_remove_from,what):
"remove pid"
something_to_remove_from.remove(what)
return something_to_remove_from
def modify(source,element,changewiththis):
"modify pid"
source[source.index(element)]=changewiththis
return source

class test(pid):
pid.original=[1,2,3]
pid.toadd=4
pid.add(pid.original,pid.toadd) # error here says that
# it expects pid instance as first arg
# not a list that it got.

why do i get an error?
--
http://mail.python.org/mailman/listinfo/python-list
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
Dec 21 '06 #5

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

Similar topics

30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
6
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make...
3
by: john | last post by:
I have a situation where i have a base class and a sub-class. A null instance of the sub-class is passed into a function. The function needs to create a new instance of the sub-class, but the...
6
by: Amjad | last post by:
Hi, I want to make a project that calls and executes a function (VB code) made in a seperate file in the Application Folder. I know I can create the function in my project and call it internally,...
2
by: sumanthsclsdc | last post by:
Hello friends, I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I...
6
by: Anthony Smith | last post by:
I can call a class using "->", but it complains about the :: I see on the net where :: is used. Is there a good explanation on when to use one over the other or the differences? $help = new...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
by: Maric Michaud | last post by:
Le Friday 05 September 2008 17:29:39 mercado mercado, vous avez écrit : Note that "self" for somemethod is just a local variable of the calling function. You cannot retrieve those local...
23
by: Stewart Berman | last post by:
I am trying to develop a wrapper class for the Windows API functions in Visual Studio 2008: GetOpenFileName GetSaveFileName I put together a starter class: using System; using...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.