0

I am realizing a porting from Java to Python. Now, I would like to define an object by using another one as neested attribute.

To be clear, taking into account the Java code as follows:

public class Foo {

    private String fooName;

    public Foo(String fooName) {
        setFooName(fooName);
    }

    public void setFooName(String fooName) {
        this.fooName = fooName;
    }

    public String getFooName() {
        return this.fooName;
    }

}

public class Bar {

    private String barName;
    private Foo foo;

    public Bar(String barName, Foo foo) {
        setBoName(fooName);
        setFoo(foo);
    }

    public void setBarName(String barName) {
        this.barName = barName;
    }

    public String getBarName() {
        return this.barName;
    }

    public void setFoo(Foo foo) {
        this.foo = foo;
    }

    public Foo getFoo() {
        return this.foo;
    }

}

How can I do just the same in Python?

3 Answers 3

7

Your classes would be as follows, note that there is no notion of public vs private so you do not need setters and getters

class Foo:
    def __init__(self, fooName):
        self.fooName = fooName

class Bar:
    def __init__(self, barName, foo):
        self.barName = barName
        self.foo = foo

As an example

>>> f = Foo('bob')
>>> b = Bar('steve', f)
>>> f.fooName
'bob'
>>> b.barName
'steve'
>>> b.foo
<__main__.Foo object at 0x02F93070>
>>> b.foo.fooName
'bob'

Again I would like to stress that the general use of setters and getters is not considered Pythonic. Either don't write/use them, or if the need arises, use @property decorators instead

Sign up to request clarification or add additional context in comments.

Comments

2

Python syntax is a bit different. It will help a lot more if you read up a bit before diving deep into it. A few things that you must take into account are:

  • Data types
  • Private/public fields/functions - pythonic way of 'suggesting' them
  • Brackets, semicolons are not considered good

But here's a demo:

class Foo(object):
    def __init__(self, fooName):
        self.setFooName(fooName)

    def setFooName(self, fooName):
        self._fooName = fooName

    def getFooName(self):
        return self._fooName

class Bar(object):
    def __init__(self, barName, foo):
        self.setBoName(fooName)
        self.setFoo(foo)

    def setBarName(self, barName):
        self._barName = barName

    def getBarName(self):
        return self._barName

    def setFoo(self, foo):
        self._foo = foo

    def getFoo(self):
        return self._foo

Comments

1

From the Python documentation:

There is a convention that is followed by most Python codes: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member).

See: https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references for more details.

As Java-based classes often "hidden" their fields (by making it private) and provides public getters/setters for this fields, you may achieve this in Python using that convention:

class Foo(object):

    def __init__ (self, fooName):
        self.setFooName(fooName)


    def setFooName(self, fooName):
        self.__fooName = fooName


    def getFooName():
        return self.__fooName;


class Bar(object) :

    def __init__(self, barName, foo) :
        self.setBarName(barName)
        self.setFoo(foo)


    def setBarName(self, barName) :
        self.__barName = barName


    def getBarName(self) :
        return self.__barName

    def setFoo(self, foo) :
        self.__foo = foo;


    def getFoo(self) :
        return self.__foo;


foo = Foo("foo")
bar = Bar("bar", foo)

print bar.getBarName()
print type(bar.getFoo())
# print bar.__barName -> this will return error as the variable is considered "private"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.