Search Results

Search found 3 results on 1 pages for 'seyz'.

Page 1/1 | 1 

  • Python - what's your conventions to declare your attributes in a class ?

    - by SeyZ
    Hello, In Python, I can declare attributes all over the class. For example : class Foo: def __init__(self): self.a = 0 def foo(self): self.b = 0 It's difficult to retrieve all attributes in my class when I have a big class with a lot of attributes. Is it better to have the following code (a) or the next following code (b) : a) Here, it's difficult to locate all attributes : class Foo: def __init__(self): foo_1() foo_2() def foo_1(self): self.a = 0 self.b = 0 def foo_2(self): self.c = 0 b) Here, it's easy to locate all attributes but is it beautiful ? class Foo: def __init__(self): (self.a, self.b) = foo_1() self.c = foo_2() def foo_1(self): a = 0 b = 0 return (a, b) def foo_2(self): c = 0 return c In a nutshell, what is your conventions to declare your attributes in a class ?

    Read the article

  • "public" or "private" attribute in Python ? What is the best way ?

    - by SeyZ
    Hi ! In Python, I have the following example class : class Foo: self._attr = 0 @property def attr(self): return self._attr @attr.setter def attr(self, value): self._attr = value @attr.deleter def attr(self): del self._attr As you can see, I have a simple "private" attribute "_attr" and a property to access it. There is a lot of codes to declare a simple private attribute and I think that it's not respecting the "KISS" philosophy to declare all attributes like that. So, why not declare all my attributes as public attributes if I don't need a particular getter/setter/deleter ? My answer will be : Because the principle of encapsulation (OOP) says otherwise! What is the best way ? Thanks !

    Read the article

  • One-to-One relation classes

    - by SeyZ
    I want to have a class named ProjectDirectory and a class named MetaDirectory. Each project has a MetaDirectory which contains some meta data. Is it the good way to write the classes like this: class ProjectDirectory(object): def __init__(self, directory=None): self.directory = directory self.meta_directory = MetaDirectory(self) def __repr__(self): return self.directory class MetaDirectory(object): def __init__(self, project_directory=None): self.project_directory = project_directory self.directory = "%s/.meta/" % project_directory ProjectDirectory has a reference to MetaDirectory and MetaDirectory has a reference to ProjectDirectory. Is there an other solution or this solution is good ?

    Read the article

1