473,624 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

private variables/methods

hi,

as far as i know in python there aren't any private (i mean not accessible
from the outside of the object) methods/fields.

why?

in java/c++ i can make a method private, this way unaccessible for the outside
world. i think it helps a lot to make for example a library more robust.

i know that there is some kind of notation to make a method/field private,
but one can still overwrite it's value.

what's the reason for this?

i'l mostly interested in the design reasons.

thanks,
gabor

--
That's life for you, said McDunn. Someone always waiting for someone
who never comes home. Always someone loving something more than that
thing loves them. And after awhile you want to destroy whatever
that thing is, so it can't hurt you no more.
-- R. Bradbury, "The Fog Horn"

Jul 18 '05 #1
27 3796
In article <ma************ *************** ******@python.o rg>, gabor wrote:
as far as i know in python there aren't any private (i mean not accessible
from the outside of the object) methods/fields.

why?


No offense, but this is like the 4000th time someone has asked that question
here. Could you try searching Google groups first?

Dave

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
Jul 18 '05 #2
On Thu, 2003-10-09 at 10:12, Dave Benjamin wrote:
In article <ma************ *************** ******@python.o rg>, gabor wrote:
as far as i know in python there aren't any private (i mean not accessible
from the outside of the object) methods/fields.

why?


No offense, but this is like the 4000th time someone has asked that question
here. Could you try searching Google groups first?


you're right, i'm sorry.

after reading the archives:

i think i didn't express myself too well.
i'll try again:

i'm aware of the fact that you can declare a variable private with "__".
but i just don't like prefix-notation. i don't like the mMember or
lpszCommandLine style notation, and also the $variable style ones.

at least for me it seemed that adding a "private" keyword somewhere is
more elegant. but there isn't anything like that.

that's why it seems to me that the designers of the language don't
recommend you to use private variables at all, but if you want, you can
use this 'hack' ( i mean the "__" notation).
thanks,
gabor
Jul 18 '05 #3
gabor wrote:

On Thu, 2003-10-09 at 10:12, Dave Benjamin wrote:
In article <ma************ *************** ******@python.o rg>, gabor wrote:
as far as i know in python there aren't any private (i mean not accessible
from the outside of the object) methods/fields.

why?


No offense, but this is like the 4000th time someone has asked that question
here. Could you try searching Google groups first?


you're right, i'm sorry.

after reading the archives:

i think i didn't express myself too well.
i'll try again:


Don't bother. You perhaps didn't really read enough of the archives,
because Dave's point still stands. None of your questions or comments,
as far as I can tell, ask or say anything that hasn't already been
asked (and answered) or said before.

In summary: it's more of a philosophical difference than anything, and
Python simply doesn't *want* a "private" keyword, nor things like that
which artificially restrict the programmer. (Again, even that comment
adds nothing new, so you're really wasting your and our time by responding
rather than continuing to read the zillion old threads on the topic.)

-Peter
Jul 18 '05 #4
Peter Hansen wrote:
gabor wrote:
On Thu, 2003-10-09 at 10:12, Dave Benjamin wrote:
In article <ma************ *************** ******@python.o rg>, gabor wrote:

as far as i know in python there aren't any private (i mean not accessible
from the outside of the object) methods/fields.

why?

No offense, but this is like the 4000th time someone has asked that question
here. Could you try searching Google groups first?
you're right, i'm sorry.

after reading the archives:

i think i didn't express myself too well.
i'll try again:


Don't bother. You perhaps didn't really read enough of the archives,
because Dave's point still stands. None of your questions or comments,
as far as I can tell, ask or say anything that hasn't already been
asked (and answered) or said before.


Because it has been asked 4000 times probably means that there is a
great need for the feature...
In summary: it's more of a philosophical difference than anything, and
Python simply doesn't *want* a "private" keyword, nor things like that
which artificially restrict the programmer. (Again, even that comment
adds nothing new, so you're really wasting your and our time by responding
rather than continuing to read the zillion old threads on the topic.)


"Python" doesn't want a "private" keyword? I have quite a limited Python
experience but I would like to have the following features in Python
that are common in other languages:

* Option Explicit
* variable type declaration (optional)
* private variables/methods

Most of these are handy for large projects, where you want to be sure
that a class is not misused (by other developers). These also mean that
it is much harder to create bugs. I like Python a lot, but with these
features it would be much better for serious development of complex
applications, not just for scripting.

One thing I have noticed that the keyword "global" is very confusing.
For example, the following is syntactically valid Python:

a = 1
def b():
a = 2
def c():
return a

But it does not work as expected. Function b just creates a new local
variable "a" inside function b! The correct function is of course:

def b():
global a
a = 2

On the other hand, function c refers to the same global variable just
fine without any extra "global" keyword. Why on earth?? :-) In every
other language I know you don't need "global". It is ugly.

Harri

Jul 18 '05 #5
Harri Pesonen wrote:
...
Because it has been asked 4000 times probably means that there is a
great need for the feature...
I can't think of ANY "feechur" from other popular languages that hasn't
been asked for, thousands of times. Does this mean that "there is a
great need" for each and all of them? Not at all: it just means that people
hanker for what they're familiar with. If Python were to satisfy even
1/10th of this incessant barrage of requests, it would devolve to a
large amorphous blob -- like many other languages have. The people
requesting these features are typically NOT experienced with Python:
they haven't experienced how the LACK of these features in fact makes
it easier and more productive to write application programs.

"Python" doesn't want a "private" keyword?
If Python can be said to have a will -- embodied in Guido or spread
as community consensus -- it definitely doesn't.
I have quite a limited Python
experience but I would like to have the following features in Python
that are common in other languages:
Not 'but', but rather, THEREFORE. Reread your words with this
change and with some luck you may get it.

* Option Explicit
* variable type declaration (optional)
* private variables/methods

Most of these are handy for large projects, where you want to be sure
that a class is not misused (by other developers). These also mean that
it is much harder to create bugs. I like Python a lot, but with these
features it would be much better for serious development of complex
applications, not just for scripting.
You are wrong. I used to harbor similar illusions (to a lesser degree,
because I _did_ have SOME experience with other dynamic languages,
but not in using them for really large apps) back when the language I
most used was C++. I was wrong, too.

Other developers aren't any likelier to "misuse" your class than you
are to misdesign it in the first place -- and you'll NEVER "be sure"
anyway, as restrictions can be worked around. _ADVISORY_
indications of "privacy" -- the convention of starting the name with
a single underscore -- are much simpler and equally effective for
your purposes. Python is wonderfully effective for programming
large applications, exactly as it is today.

One thing I have noticed that the keyword "global" is very confusing.
You are right. It would be better if the current module could be
imported -- by using some reserved special module name in a
perfectly ordinary 'import' instruction -- so that global variables
could then be re-bound as attributes of this module.

Just to give you an idea, in today's Python you could add this
feature as:

# part to be executed once, e.g. in site.py
import __builtin__, sys
_base_import = __builtin__.__i mport__
def __import__(name , *args):
if name == '__current_modu le__':
name = sys._getframe(1 ).f_globals['__name__']
return _base_import(na me, *args)
__builtin__.__i mport__ = __import__
# end of part to be executed once

# example use
x = 23

def set_the_global( ):
import __current_modul e__
__current_modul e__.x = 45

print x
set_the_global( )
print x
emits
23
45
For example, the following is syntactically valid Python:

a = 1
def b():
a = 2
def c():
return a

But it does not work as expected. Function b just creates a new local
variable "a" inside function b! The correct function is of course:

def b():
global a
a = 2

On the other hand, function c refers to the same global variable just
fine without any extra "global" keyword. Why on earth?? :-) In every
Because reading is different from writing. Reading globals is (more or
less) all right; writing globals is a delicate decision that is well worth
emphasizing. So, anything that's written (any name that's re-bound,
to be precise) is deemed to be local -- unless explicitly mentioned in
a global statement.

The problem with global is that it's not clear enough. If there simply
was NO way at all to have any assignment to a bare name, such
as "a=2", EVER affect anything BUT a local, things would be much
clearer; the need to import __current_modul e__ would emphasize what
a serious, think-twice-about-it decision it is to choose to rebind
module-global names. It would also emphasize that 'global' means
'of this module', not in any way of ALL modules -- a misconception
that often affects newbies.

Hmmm -- maybe THIS is worth proposing for 2.4 (with a
pending deprecation warning for the global statement)...
other language I know you don't need "global". It is ugly.


Problem is, it's not quite ugly enough (nor quite clear enough).
Discouraging you from affecting globals is a swell idea, but I
think the 'global' statement may not be enough for that, whence
my newly conceived suggestion about importing...
Alex

Jul 18 '05 #6
Alex Martelli wrote:
...
I can't think of ANY "feechur" from other popular languages that hasn't
been asked for, thousands of times....

Aha! caught the Martellibot in a rare memory failure (perhaps a rare
double-bit parity error)? To my knowledge, nobody has suggested the
autodeclaration of variables which begin with the letters 'I' through
'N' as integer variables. So there. :-)

Now watch, he'll document 1003 requests on alt.lang.python .it.

-Scott David Daniels
Sc***********@A cm.Org

Jul 18 '05 #7
On Fri, 10 Oct 2003 22:22:04 GMT, Alex Martelli <al*****@yahoo. com> wrote:
The problem with global is that it's not clear enough. If there simply
was NO way at all to have any assignment to a bare name, such
as "a=2", EVER affect anything BUT a local, things would be much
clearer; the need to import __current_modul e__ would emphasize what
a serious, think-twice-about-it decision it is to choose to rebind
module-global names. It would also emphasize that 'global' means
'of this module', not in any way of ALL modules -- a misconception
that often affects newbies. Hmmm -- maybe THIS is worth proposing for 2.4 (with a
pending deprecation warning for the global statement)...


I'm rather amused at the thought of a module importing itself. I find it
cleaner than global, and it also emphasizes that modules are singletons,
if you think about how a module *can* import itself.

I think that __current_modul e__ is perhaps a bit too lengthy (although I
appreciate the motivation behind this), but aside from that, I like it.

I'm also pleased with the ouroboros-like imagery it conjures..

J.
Jul 18 '05 #8
Harri Pesonen fed this fish to the penguins on Friday 10 October 2003
12:16 pm:


Because it has been asked 4000 times probably means that there is a
great need for the feature...
Or it just means that there are 4000 undisciplined programmers out
there who can't trust their own coding and require the language to
protect them from themselves.

For them, I suggest coding in Ada...

"Python" doesn't want a "private" keyword? I have quite a limited
Python experience but I would like to have the following features in
Python that are common in other languages:

* Option Explicit
Only found in M$ Basic dialects as I recall. Real BASIC only required
declarations for arrays (needed the size), and used special suffix
characters for type identification.
* variable type declaration (optional)
Well, if you posit an option explicit statement, being required to
declare variables would become an option...
* private variables/methods

Why? You don't trust yourself to stay away from "internal" details
when using a class?

Let's see... Languages that don't require variable declarations:

BASIC (though it does allow suffix character to differentiate number
from string)
FORTRAN (though it uses variables beginning I-N as integers, all others
are real)
Python (no restriction, any variable can refer to any type of object)
REXX (no restriction that I know of)
DCL (and most command line scripting languages)
APL
LISP (at least, the versions up to the early 80s)
FORTH

Languages that require declarations for all variables:

Ada
COBOL
C++
Java
assembly (well, you have to declare the storage space at least)

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #9
Scott David Daniels fed this fish to the penguins on Friday 10 October
2003 17:09 pm:

Now watch, he'll document 1003 requests on alt.lang.python .it.
No... What he'll find are requests that variables beginning A-H and
O-Z be floats....

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #10

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

Similar topics

3
21382
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have an idea that we can have private constructors and destructors but am not able to find a situation where they can be used... Regards RVG rajeshgarg@opussoft.com
1
23178
by: Edward | last post by:
I have trouble with some of the concepts of OOP, and am struggling currently with Private Shared Functions. I think I understand Private (not available outside the class). I think I understand Shared (available without having to instantiate a class). So how could a Private Shared Function be called? Why give it this particular scope?
6
1507
by: David Whitchurch-Bennett | last post by:
Hi There, I have created a very simple web user control, containing only a combo box. I have created a class which contains some private object variables, for example... Private _anObject as Object Public Property anObject() As Object Get
86
4607
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere."
63
2645
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears to me (perhaps wrongly) that Python prefers to leave class data public. What is the logic behind that choice? Thanks any insight.
7
1358
by: Ray | last post by:
Hello, What do you think about emulating private variables for a class this way? function Something() { var private; Something.prototype.getPrivate = function() { return private; } Something.prototype.setPrivate = function(newPrivate) { private = newPrivate; }
8
5733
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to private methods in the same class as method arguments. For example, if Public method Foo() calls private method Bar(), and if Bar() uses member variable m_MyVariable, I declare the private method with the signature: private void Bar(SomeType...
13
1717
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came up with this: //base private object constructor function priv(){ this.a = 1; this.b = 2;
14
1555
by: Karsten Dambekalns | last post by:
Hi. Thomas Mlynarczyk wrote: Why do you want them to be private in the first place? I have yet to see code where this really makes sense... And if it does, then the external public API is what counts, if the code as a whole still delivers what it promises, and that can be tested, it should be fine.
0
8240
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...
1
8336
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,...
0
8482
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 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...
0
5565
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
4082
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
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
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
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
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...

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.