473,396 Members | 2,158 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,396 software developers and data experts.

question about class, functions and scope

lo there all,

i have an app that runs three classes as threads in the same program.
some of them need to be able to share some functions. Can i set a
function before i define a class and have the class use it ? Kinda like
this.

def some_function(var1, var2):
do something with var1, var2
return result

class do_something1(threading.Thread):
def __init__(var):
do something
def run():
var1 = 3
var2 = 4
result = some_function(var1,var2)

is this legal ? is it pythonic?
i ask because i plan to do a big re-write soon, and want to get rid of
some repetition

thanks

Aug 26 '06 #1
7 1457
nephish:
is this legal ? is it pythonic?
It's legan and pythonic. Functions are here for a purpose.

Bye,
bearophile

Aug 26 '06 #2

be************@lycos.com wrote:
nephish:
is this legal ? is it pythonic?

It's legan and pythonic. Functions are here for a purpose.

Bye,
bearophile
cool enough, and thanks for the quick reply.

shawn

Aug 26 '06 #3

nephish wrote:
be************@lycos.com wrote:
nephish:
is this legal ? is it pythonic?
It's legan and pythonic. Functions are here for a purpose.

Bye,
bearophile

cool enough, and thanks for the quick reply.

shawn
one more question.
the functions defined above the classes that the could be called from
within the classes, they do not need a 'self' declaration because they
are not part of a class, right?

sk

Aug 26 '06 #4
nephish:
one more question.
the functions defined above the classes that the could be called from
within the classes, they do not need a 'self' declaration because they
are not part of a class, right?
Class methods generally require the self as first parameter, functions
don't need the self. So your original code was wrong (sorry, I haven't
seen that before).

You can also inject functions as methods inside a class, and in such
case your function usually needs a self parameter too.

Bye,
bearophile

Aug 26 '06 #5

be************@lycos.com wrote:
nephish:
one more question.
the functions defined above the classes that the could be called from
within the classes, they do not need a 'self' declaration because they
are not part of a class, right?

Class methods generally require the self as first parameter, functions
don't need the self. So your original code was wrong (sorry, I haven't
seen that before).

You can also inject functions as methods inside a class, and in such
case your function usually needs a self parameter too.

Bye,
bearophile
ok, thanks much, thats all i needed to know.
shawn

Aug 26 '06 #6
At Saturday 26/8/2006 06:13, nephish wrote:
>i have an app that runs three classes as threads in the same program.
some of them need to be able to share some functions. Can i set a
function before i define a class and have the class use it ? Kinda like
this.

def some_function(var1, var2):
do something with var1, var2
return result
It's ok - but beware of concurrency problems. By example, two threads
trying to update the same thing. (Doesn't happen in your small example).

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Aug 28 '06 #7
At Monday 28/8/2006 14:55, shawn bright wrote:
>would it be better to move all the functions i want to share into
some class and have the class threads refer to them that way ? i
mean, just to keep things seperate, but resuseable ?
That really depends on your design. Python does not enforce that
*all* code be in class methods; using functions alone -as others have
pointed out- is fine.

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Aug 28 '06 #8

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

Similar topics

4
by: Joe | last post by:
The recipe in question is "Implementing Static Methods". It shows how to use staticmethod(). This sentence in the Discussion section isn't clear to me: "An attribute of a class object that...
2
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
55
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.