I found that in python we have no realy private attributes.we just can use _ or __.but these are not deny access to my attributes.some thing such as private variables in c++.
Is it TRUE? Realy python is week in these?

Go ahead and test it!
If you can't live with the amount of protection given, use C++.

It is in fact true. Nothing is truly private in Python. What happens is that Python alters the name of the variable internally.

>>> class Foo:
	__privatebar = "HelloWorld!"

	
>>> Foo.__privatebar

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    Foo.__privatebar
AttributeError: class Foo has no attribute '__privatebar'

>>> Foo._Foo__privatebar
'HelloWorld!'
>>>

But PLEASE! Do not ever do this. Just imagine that the python god will come and hit you if you do this.

This is not weak. Python doesn't encourage bad code, but they don't deny it. If you want to do something stupid like this, don't come here to ask us to debug it for you.

To confuse variable Foo.__privatebar with Foo._Foo__privatebar would be a hard thing to do. As you can see, Python uses name mangling to protect a private variable.

I tried to say my friend that python is as powerful as java.but they said me this problem.and the other was java's machine.it wor4k on every thing but python...
I don't know.I am using python since 2 years.I choosed it and I LOVE IT.
But we have to understand python's abilities.is not True.

There really are good reasons to prefer one language or another for a particular task. There is no good way to make a general decision about what language is 'better' than another without more specification.

This (I think it is silly) discussion seems to be a theme for the last few days. End of term syndrome?

So we have no private attribute as C++ in python.yes?
has python any thing about machine for hardware managmente or some thing like?
for example if we want to write a program that works on a set such as CNC or other sets such as this?

Direct hardware access is not Python's great strength, but yes, it is possible using the ctypes module, or if you really want it your way, by providing a C or C++ extension.

When I started as a C++ programmer coming from C, I thought that having private, protected and public class members gave you a lot of good control over the way your classes were used. The longer I worked with C++ (and as I learned and began using Java), the more I found that I wanted to give the users of my class more not less control. Yes, it is good to have accessors, so you can decorate or instrument them if for no other reason, but if you don't trust the 'downstream users' to do the right thing, you end up spending way too much time armor plating your code. As I now see it, you document document document, but you don't lock things up. This may be partly because now I'm informed by the way Python does things, but I think it is also a life lesson learned.
In my opinion, Python's strengths are:

  • Universally available (almost)
  • Easy to begin using
  • Easy to get to better at using
  • Easy to maintain and understand
  • First class "everything"
  • Lots of libraries (another sign of universality: Lots of people have already used it)
  • Good access to the underlying OS (like C and C++, but easier)
  • Fast development cycle (no compile or link step) with good unit test harness
  • Straight forward syntax (no surprises)
  • Duck typing (http://en.wikipedia.org/wiki/Duck_typing)
  • Plays well with other languages
  • Scales well from small one-minute jobs to multi-developer cross platform major application development

... but It is neither small nor fast, so it doesn't belong where small or fast matters more than quick and easy from the developer's perspective. Though it is good as the user interface layer above optimized code that does the heavy lifting.

So we have no private attribute as C++ in python.yes?
has python any thing about machine for hardware managmente or some thing like?
for example if we want to write a program that works on a set such as CNC or other sets such as this?

Yes, Python has private attributes, but it uses "name mangling" to implement it. For all practical purposes this is good enough!

Private attributes are stupid for dynamic languages.
Don't you see Python is dynamic and strongly typed,
and C++ is dynamic and strongly typed?
Python doesn't have them for a reason.
It contributes to the beauty of the Python language.
So in other words, it is a feature.

Even private attributes in static languages are not absolutely private.
In C#, I know that you can use Reflection to use private methods from another class.

jcao219's post confuses me: Python and C++ share "dynamic" and "strongly typed" but C# is apparently "static" with no mention of type. Perhaps jcao219 could re-post using more usual terminology?

I agree that the lack of private and protected in Python is a feature, but perhaps not for the same reasons: I like it because it promotes the use of Python to do its own introspection for things like serialization and debugging, things which are harder to do in languages that try to control access to some class members. And that 'eat your own dog food' effect is something that I do see as elegant / beautiful.

Whoops, I made a mistake.
C++ is static.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.