473,399 Members | 3,919 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,399 software developers and data experts.

How do these Java concepts translate to Python?

Ray
Hello,

I've been learning Python in my sparetime. I'm a Java/C++ programmer by
trade. So I've been reading about Python OO, and I have a few questions
that I haven't found the answers for :)

1. Where are the access specifiers? (public, protected, private)
2. How does Python know whether a class is new style or old style?
E.g.:

class A:
pass

How does it know whether the class is new style or old style? Or this
decision only happens when I've added something that belongs to new
style? How do I tell Python which one I want to use?

3. In Java we have static (class) method and instance members. But this
difference seems to blur in Python. I mean, when you bind a member
variable in Python, is it static, or instance? It seems that everything
is static (in the Java sense) in Python. Am I correct?

Thanks in advance,
Ray

Aug 12 '05 #1
22 1606

Ray wrote:
1. Where are the access specifiers? (public, protected, private)


AFAIK, there is not such a thing in Python.

---Fausto
Aug 12 '05 #2

Fausto Arinos Barbuto wrote:
Ray wrote:
1. Where are the access specifiers? (public, protected, private)


AFAIK, there is not such a thing in Python.

---Fausto


Well, technically you can use _attribute to mangle it, but technically
speaking, there are no public, protected, or private things.

Aug 12 '05 #3
Ray
Fausto Arinos Barbuto wrote:
Ray wrote:
1. Where are the access specifiers? (public, protected, private)
AFAIK, there is not such a thing in Python.


So everything is public? I know that you can prefix a member with
underscores to make something private, but how about protected, for
example?

---Fausto


Aug 12 '05 #4
Ray
Devan L wrote:
Fausto Arinos Barbuto wrote:
Ray wrote:
1. Where are the access specifiers? (public, protected, private)


AFAIK, there is not such a thing in Python.

---Fausto


Well, technically you can use _attribute to mangle it, but technically
speaking, there are no public, protected, or private things.


OK, thanks. How about static members and instance members? Seems that
in Python everything is class-wide?

Thanks
Ray

Aug 12 '05 #5
Ray wrote:
1. Where are the access specifiers? (public, protected, private)
There's no enforceable way of doing these. The convention is that names
that begin with a single underscore are private to the
class/module/function/etc. Hence why sys._getframe() is considered a
hack -- it's not officially part of the sys module.
2. How does Python know whether a class is new style or old style?
E.g.:

class A:
pass
That's an old-style class. All new-style classes inherit from object
(or another new-style class). So write it like:

class A(object):
pass

If you're just learning Python now, there's really no reason to use
old-style classes. They're only there for backwards compatibility.
3. In Java we have static (class) method and instance members. But this
difference seems to blur in Python. I mean, when you bind a member
variable in Python, is it static, or instance? It seems that everything
is static (in the Java sense) in Python. Am I correct?


No, there's a difference, but things can get a little tricky. We'll
start with the easy one:

py> class A(object):
.... x = 0 # "static", class-level
.... def __init__(self):
.... self.y = 1 # "non-static", instance-level
....

The difference between a "static", class-level attribute and a
"non-static", instance-level attribute is whether it's set on the class
object (e.g. in the class definition), or on the instance object (e.g.
by writing self.XXX = YYY). Playing with this a bit:

py> a = A()
py> A.x # read the class-level "x"
0
py> a.x # read the *class*-level "x" (but search through the instance)
0
py> A.y # try to read a class-level "y"
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: type object 'A' has no attribute 'y'
py> a.y # read the instance-level "y"
1

Note that you can read "static" attributes from both the class and the
instance, and you can only read "non-static" attributes from the
instance, just like Java (IIRC).

Where you're likely to get confused is that instance level attributes
can shadow the class level attributes. That is, if you set an instance
attribute with the same name as a class attribute, you can't find the
class attribute through that instance anymore:

py> a.x = 2 # set the *instance*-level "x"
py> A.x # read the *unchanged* class-level "x"
0
py> a.x # read the *changed* instance-level "x"
2

HTH,

STeVe
Aug 12 '05 #6
Ray wrote:
Devan L wrote:
Fausto Arinos Barbuto wrote:
Ray wrote:
1. Where are the access specifiers? (public, protected, private)

AFAIK, there is not such a thing in Python.

---Fausto


Well, technically you can use _attribute to mangle it, but technically
speaking, there are no public, protected, or private things.

OK, thanks. How about static members and instance members? Seems that
in Python everything is class-wide?


You can define instance data in the __init__ method.

def __init__(self):
self.instance_member = 0;
Aug 12 '05 #7
Please look through this example code, and the comments. If I've
misspoken, please anyone correct my errors.

-- Paul
class OldStyleClass:
"""A definition of an old style class."""
pass

class NewStyleClass(object):
"""Note that NewStyleClass explicitly inherits from object. This
is what makes it new-style."""
pass

class B(object):
pass

class C(object):
pass

class A(B,C):
"""Class A inherits from classes B and C. Since they are new-style,
A is new-style, too."""

# this is a class variable of A.
classVar = 0

def __init__(self,initArgs=None):
"""This string documents this routine, which is the initializer

for new instances. __init__ is not typically explicitly
called (except from subclasses), but is automatically called

when creating new instances of class A, as in:
aObj = A(initWithThisValue)
Since initArgs is declared with a default value, it is also
possible to create an object as:
aObj = A()
and __init__ will be invoked with None as the value of
initArgs.
"""
if initArgs is not None:
self.instanceVar = initArgs
else:
self.instanceVar = 0

@staticmethod
def staticMethod(a,b,c):
"""This is a class-level method. Presumably it has something
to do with this class, perhaps as a factory or other
utility method.

This method is invoked as:
A.staticMethod(100, ['A','B'], 3.14159)

What makes this a static method is the @staticmethod
decorator that precedes the class definition.
(Pre-2.4 code would use the form:
staticMethod = staticmethod(staticMethod)
in place of the @staticmethod decorator.)
"""
pass

@classmethod
def classMethod(cls,d,e,f):
"""This is also a class-level method, but is distinct in that
the class is implicitly passed as the first argument,
although the caller does not pass the class in.

This method looks similar to the static method invocation:
A.classMethod(5,'XYZZY',[])

But in this case, the variable cls takes the value of the
class used to invoke the method, either A or some subclass
of A.
"""
print cls,type(cls)

def instanceMethod(self,g,h,i):
"""By default, this method is assumed to be an instance
method. The first argument in the list is the object's
own reference variable - by convention this is near-
universally named 'self', although some prefer the
variable name '_'. Any variable will do really, such
as 'me', 'this', 'I', etc., but it is the first variable
in the list.

The caller does not explicitly pass this object reference
variable in the calling arg list. Invoking instanceMethod
looks like:
aVar = A()
aVar.instanceMethod(1,2,3)
"""
pass

def __hiddenMethod(self,x,y,z):
"""By the magic of the leading '__' on this method name,
this method is not externally visible. It *can* be
called from derived classes, though, so it can be thought
of as roughly analogous to being a 'protected' method
in C++ or Java (although as I recall, 'protected' in
Java isn't really all that protected).

Leading '__' can also be used to hide instance and class
vars, too.
"""
pass

# Here is how you define a class-level variable of A that is of type A.
# You could use these to predefine special A variables, as in
# simulating an enum, or in creating some common values of a given
# class, such as Color.RED, Color.GREEN, etc.
A.specialA = A("special")
A.unusualA = A("unusual")

class G(A):
"""A subclass of A, used to demonstrate calling a classmethod,
and a hidden method."""
pass

def tryHidden(self):
# call a method defined in the superclass
self.__hiddenMethod(4,5,6)

# Invoke some class-methods. The first call will pass the class A
# as the first arg, the second will pass the class G as the first arg.
A.classMethod(1,2,3)
G.classMethod(4,5,6)

g = G()
g.tryHidden() # Allowed
g.__hiddenMethod(5,6,7) # Not allowed!

Aug 12 '05 #8
Ray <ra********@yahoo.com> wrote:
1. Where are the access specifiers? (public, protected, private)
No such thing (or, if you like, everything is "private" by default).

By convention, "please don't access this name externally" is indicated
by using the name '_foo' instead of 'foo'; similar to a "protected".
Nothing in the language enforces this.

Recently, the language came to partially support '__foo' (i.e. a name
beginning with two underscores) as a pseudo-"private". It's just a
namespace munging though; sufficiently determined users can get at it
without much effort.

The attitude engendering this simplicity is "we're all consenting
adults here". If you have users of your modules and classes who won't
respect access restriction *conventions*, they're bad programmers
anyway, so there's not much the language can do to stop that.
2. How does Python know whether a class is new style or old style?
E.g.:

class A:
pass
New-style classes are descended from class 'object'. Old-style classes
aren't.

Thus, your example above is an old-style class, as are any classes
that inherit only from that class.

To create a new-style class with no particular base functionality,
inherit from 'object' directly:

class A(object):
pass
3. In Java we have static (class) method and instance members. But
this difference seems to blur in Python.


Class attributes and instance attributes are distinguished by the fact
that instance attributes are created when the instance is created
(typically, assigned within the __init__() method):

class A(object):
foo = 'Fudge'
def __init__(self, bar):
self.bar = bar

wibble = A('Wibble')
print wibble.foo, wibble.bar # Fudge Wibble
bobble = A('Bobble')
print bobble.foo, bobble.bar # Fudge Bobble

Instances of the 'A' class all share a 'foo' attribute, and each
instance has its own 'bar' attribute created separately (in the
__init__() method).

--
\ "Unix is an operating system, OS/2 is half an operating system, |
`\ Windows is a shell, and DOS is a boot partition virus." -- |
_o__) Peter H. Coffin |
Ben Finney <http://www.benfinney.id.au/>
Aug 12 '05 #9
Instance variables are typically defined in __init__(), but they can be
added to an object anywhere. The only exception is when defining the
magic __slots__ class variable to pre-define what the allowed instance
variables can be.

class A:
pass

a = A()
a.instVar1 = "hoo-ah"
a.instVar2 = "another"

Try that in C++ or Java!

-- Paul

Aug 12 '05 #10
Ray
Thanks guys! Your explanations have cleared up things significantly.

My transition from C++ to Java to C# was quite painless because they
were so similar, but Python is particularly challenging because the
concepts are quite different. (I always have this paranoid feeling: "Am
I using Python to write Java, or using Python to write Python?")

Regards,
Ray
Ray wrote:
<snipped>

Aug 12 '05 #11
Ray wrote:
Hello,

I've been learning Python in my sparetime. I'm a Java/C++ programmer by
trade. So I've been reading about Python OO, and I have a few questions
that I haven't found the answers for :)

1. Where are the access specifiers? (public, protected, private)
object.name => public
object._name => protected
object.__name => private
2. How does Python know whether a class is new style or old style?
E.g.:

class A:
pass
This is an old-style class.
How does it know whether the class is new style or old style? Or this
decision only happens when I've added something that belongs to new
style? How do I tell Python which one I want to use?
class B(object): # or any subclass of object
pass
3. In Java we have static (class) method and instance members. But this
difference seems to blur in Python. I mean, when you bind a member
variable in Python, is it static, or instance?
Depends if you bind it to the class or to the instance !-)
It seems that everything
is static (in the Java sense) in Python. Am I correct?
No.

class Foo(object):
bar = 42 # this is a class variable

# __init__ is the equivalent of Java constructors
def __init__(self, baaz):
self.baaz = baaz # this is an instance variable

# this is a class method
# (the first argument is the class object, not the instance)
@classmethod
def bak(cls, frooz):
cls.bar = min(cls.bar, frooz) + 1138

# default is instance method
def zoor(self):
print "%s %d" % (self.baaz, Foo.bar)
Thanks in advance,


HTH

--
bruno desthuilliers
ruby -e "print 'o****@xiludom.gro'.split('@').collect{|p|
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Aug 12 '05 #12
Devan L wrote:
Fausto Arinos Barbuto wrote:
Ray wrote:

1. Where are the access specifiers? (public, protected, private)
AFAIK, there is not such a thing in Python.

---Fausto

Well, technically you can use _attribute to mangle it,


__attribute would work better !-)
but technically
speaking, there are no public, protected, or private things.


Yes there are:
object.name is public
object._name is protected
object.__name is private

You don't need the language to enforce this, it's just a matter of
conventions.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Aug 12 '05 #13
Ray wrote:
Fausto Arinos Barbuto wrote:
Ray wrote:

1. Where are the access specifiers? (public, protected, private)
AFAIK, there is not such a thing in Python.

So everything is public? I know that you can prefix a member with
underscores to make something private,


The "2 leadings underscore name mangling" scheme is intented to protect
"sensible" attributes from being accidentally overriden by derived
classes, not to prevent access to the attribute.

class Foo(object):
def __init__(self):
self.__baaz = 42

f = Foo()
print f._Foo__baaz
but how about protected, for
example?


object._protected_attribute is enough to tell anyone not to mess with
this attribute. Believe it or else, but this happens to work perfectly.

And BTW, don't bother making all your attributes "protected" or
"private" then writing getters and setters, Python has a good support
for computed attributes, so you can change the implementation without
problem (which is the original reason for not-public attributes):

# first version:
class Foo(object):
def __init__(self):
self.baaz = 42 # public attribute

# later we discover that we want Foo.baaz to be computed:
class Foo(object):
def __init__(self):
self.baaz = 42

def _set_baaz(self, value):
if value < 21 or value > 84:
raise ValueError, "baaz value must be in range 21..84"
self._baaz = value

def _get_baaz(self):
return self._baaz * 2

baaz = property(fget=_get_baaz, fset=_set_baaz)

Easy as pie, uh ?-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Aug 12 '05 #14
"Ray" <ra********@yahoo.com> wrote:
I've been learning Python in my sparetime. I'm a Java/C++ programmer by
trade. So I've been reading about Python OO, and I have a few questions
that I haven't found the answers for :)

1. Where are the access specifiers? (public, protected, private)


Quick answer; there are none, all attributes are public.

Slightly longer answer; if you name an attribute with two leading
underscores (i.e. "__myPrivateData"), there is some name mangling that goes
on which effectively makes the attribute private. There are ways around
it, but you have to know what you're doing and deliberately be trying to
spoof the system (but, then again, exactly the same can be said for C++'s
private data).

Soapbox answer; private data is, in some ways, a useful tool, but it is not
part and parcel of object oriented programming. I've had people (mostly
C++/Java weenies) that Python is not an OOPL because it does not enforce
data hiding. "Feh", I say to them.
Aug 12 '05 #15
Ray


bruno modulix wrote:
<snipped>
And BTW, don't bother making all your attributes "protected" or
"private" then writing getters and setters, Python has a good support
for computed attributes, so you can change the implementation without
problem (which is the original reason for not-public attributes):
Thanks Bruno! This is good stuff. This is exactly what I want to avoid:
writing Java in Python.

Cheers,
Ray

# first version:
class Foo(object):
def __init__(self):
self.baaz = 42 # public attribute

# later we discover that we want Foo.baaz to be computed:
class Foo(object):
def __init__(self):
self.baaz = 42

def _set_baaz(self, value):
if value < 21 or value > 84:
raise ValueError, "baaz value must be in range 21..84"
self._baaz = value

def _get_baaz(self):
return self._baaz * 2

baaz = property(fget=_get_baaz, fset=_set_baaz)

Easy as pie, uh ?-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"


Aug 12 '05 #16
Ray
Roy Smith wrote:
Quick answer; there are none, all attributes are public.

Slightly longer answer; if you name an attribute with two leading
underscores (i.e. "__myPrivateData"), there is some name mangling that goes
on which effectively makes the attribute private. There are ways around
it, but you have to know what you're doing and deliberately be trying to
spoof the system (but, then again, exactly the same can be said for C++'s
private data).
Well yeah... if you really want it, in Java you can do that too via
reflection. Just that I'm not used to it yet so I feel a bit jittery
with so much power on my hands!
Soapbox answer; private data is, in some ways, a useful tool, but it is not
part and parcel of object oriented programming. I've had people (mostly
C++/Java weenies) that Python is not an OOPL because it does not enforce
data hiding. "Feh", I say to them.


Feh... those weenies don't know what they're talkin about.

Aug 12 '05 #17
In article <dd**********@rose.polar.local>,
Ben Finney <bi****************@benfinney.id.au> wrote:

Recently, the language came to partially support '__foo' (i.e. a name
beginning with two underscores) as a pseudo-"private". It's just a
namespace munging though; sufficiently determined users can get at it
without much effort.


Recently?
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

The way to build large Python applications is to componentize and
loosely-couple the hell out of everything.
Aug 12 '05 #18
Ray wrote:
Roy Smith wrote:
Quick answer; there are none, all attributes are public.
(snip)
Well yeah... if you really want it, in Java you can do that too via
reflection. Just that I'm not used to it yet so I feel a bit jittery
with so much power on my hands!


Then wait until you discover what one can do with __magic_methods__,
functions-as-objects, closures, callable objects, descriptors
(properties on steroids), decorators, generators, and metaclasses...
*Then* you'll know what power means !-)

And more is to come...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Aug 12 '05 #19
Ray
bruno modulix wrote:
Then wait until you discover what one can do with __magic_methods__,
functions-as-objects, closures, callable objects, descriptors
(properties on steroids), decorators, generators, and metaclasses...
*Then* you'll know what power means !-)

And more is to come...
.... I've got a feeling that this Pythonic journey will expand my brain
so much ;)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"


Aug 12 '05 #20
bruno modulix wrote:
but technically
speaking, there are no public, protected, or private things.


Yes there are:
object.name is public
object._name is protected
object.__name is private


The double-underscore name-mangling is almost never worth it. It's
supposed to stop name collisions, but, while it does in some cases, it
doesn't in all cases, so you shouldn't rely on this. For example:

---------- mod1.py ----------
class C(object):
__x = 'mod1.C'
@classmethod
def getx(cls):
return cls.__x
-----------------------------

---------- mod2.py ----------
import mod1
class C(mod1.C):
__x = 'mod2.C'
-----------------------------

py> import mod1, mod2
py> mod1.C.getx()
'mod1.C'
py> mod2.C.getx()
'mod2.C'

If double-underscore name-mangling worked like private variables,
setting C.__x in mod2.C should not affect the value of C.__x in mod1.C.

STeVe
Aug 12 '05 #21
Ray
gene tani wrote:
I think you'll like python.

http://naeblis.cx/rtomayko/2004/12/1...c-method-thing
http://dirtsimple.org/2004/12/java-i...on-either.html

(and python-is-not-java)


Thanks gene, these are exactly the stuff I wanna know more about.

Ray

Aug 13 '05 #23

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

Similar topics

4
by: inquirydog | last post by:
Hello- I, the inquirydog, would like to solicit suggestions for a new web page I am making: I am creating a simple website that will translate concepts between windows os's, Linux, and the...
2
by: Dave Brueck | last post by:
Below is some information I collected from a *small* project in which I wrote a Python version of a Java application. I share this info only as a data point (rather than trying to say this data...
8
by: Ray | last post by:
Hello there, I've been programming in Java for about 8 years now, but lately I've been hearing a lot about Python and I'm really interested in learning more about it. I've read the tutorial, and...
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
12
by: john_sips_tea | last post by:
I've got a fairly substantial webapp written in Java (plus Tomcat, Hibernate, Struts, JSP, MySQL) that is a bit of a bear to work with. I didn't write it. Much of it is only very sparsely...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
63
by: s0suk3 | last post by:
I've been programming Python for a couple of years now. Now I'm looking to move on to either C++ or Java, but I'm not sure which. Which one do you think will be a better transition for a Python...
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: 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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.