getter

Pythonic alternative to dict-style setter?

♀尐吖头ヾ 提交于 2021-01-28 05:06:15
问题 People tend to consider getters and setters un-Pythonic, prefering to use @property instead. I'm currently trying to extend the functionality of a class that uses @property to support a dict: class OldMyClass(object): @property def foo(self): return self._foo @foo.setter def foo(self, value): self.side_effect(value) self._foo = value class NewMyClass(object): @property def foo(self, key): # Invalid Python return self._foo[key] @foo.setter def foo(self, key, value): # Invalid Python self.side

What is the simplest way to define setter and getter in Python

不羁的心 提交于 2021-01-27 05:01:52
问题 What is the simplest way to define setter and getter in Python? Is there anything like in C# public int Prop {get; set;} How to make it like this? Since to write both setter and getter methods for one property like this is just too much work. class MyClass(): def foo_get(self): return self._foo def foo_set(self, val): self._foo = val foo = property(foo_get, foo_set) Thanks in advance! 回答1: If the setter and getter do nothing else than accessing an underlying real attribute, then the simplest

Returning mutable member variables (Date/Timestamp) using getter functions in Java? [duplicate]

不打扰是莪最后的温柔 提交于 2020-08-08 05:02:50
问题 This question already has answers here : FindBugs : real threat behind EI_EXPOSE_REP (2 answers) What is the point of getters and setters? [duplicate] (13 answers) Closed 3 years ago . I have a java class: class MyObj{ private Timestamp myDate; public Timestamp getMyDate(){ return mydate; } ... } when I check it by Findbugs, it says: Bug kind and pattern: EI - EI_EXPOSE_REP May expose internal representation by returning reference to mutable object so, what is the better way to write the

Setter Getter Arrays Java

帅比萌擦擦* 提交于 2020-07-07 23:29:17
问题 Can somebody help me with one little problem. I want to set for example 3 lectures to 1 student, but when i try this i can't set lectures. student.setStudentLecture(lecture); student.setStudentLecture(lecture1); public class Student { private Lecture[] lecture; public void setStudentLecture(Lecture[] lecture) { this.lecture = lecture; } public Lecture[] getStudentLecture() { return lecture; } } 回答1: You are using Array of Lecture objects and overwriting the same array with two different array

How to pass values using setter and getter among three classes

落花浮王杯 提交于 2020-06-26 06:39:07
问题 I've been practicing a project which is the classical Nim game. What I've achieved now is: Add, remove, edit, display, players. (Nimsys and NimPlayer) Selecting two players to play a game. (NimGame class) Every time when the game ends, I need to return these two things from NimGame to NimPlayer. Then I can use getter in Nimsys: If the player wins, his/her score +1. Every time after a game, the number of game +1 for the player who played. What I've already tried was to pass the "score" and

Lombok getter/setter vs Java 14 record

不羁岁月 提交于 2020-05-12 11:39:12
问题 I love project Lombok but in these days I'm reading and trying some of the new features of java 14. Inside the new capability, there is the record keyword that allows creating a class with already built-in the following functionality: constructor, private final fields, accessors, equals/hashCode, getters, toString methods. Now my question is: is better to rely on the feature of Lombok or should we start using the record functionality: Is better to use this: record Person (String name, String