Mutable variable in a class - python

If I have a class like this.
class Person():
def __init__(self, name):
self._name = name
self._name_list = []
if(self._name not in self._name_list):
self._name_list.append(self._name)
father = Person("Michael")
mother = Person("Sharon")
>>>self._name_list
["Michael", "Sharon"]
How can I do this without creating a global variable? Everytime I instantiate a new person it creates their own list. But I need a list inside the scope of the class that appends a name everytime a new person is created.

You can save it inside the class itself, like this:
class Person():
_name_list = []
def __init__(self, name):
self._name = name
if self._name not in self._name_list:
self._name_list.append(self._name)
father = Person("Michael")
mother = Person("Sharon")
print(Person._name_list)
outputs:
['Michael', 'Sharon']

You can try this:
people = []
class Person():
def __init__(self, name):
self._name = name
if self._name not in people:
global people
people.append(self._name)
person = Person('name1')
person1 = Person('name2')
print(people)
Output:
['name1', 'name2']

Related

Python updating list from constructor by object.attribute

If a class is created with the attributes: name and list and the name attribute has a default value and the list is appending the name. Is it possible in somehow when I create an object "a" and type
"a.name = 'x' " that this 'x' will appear in the list given that the list is appending in the constructor?
class Person:
list = []
def __init__(self, name="Zed"):
self.name = name
self.list.append(name)
def printList(self):
print(self.list)
a = Person()
a.name = "Yasuo"
a.printList() #outputs Zed but Yasuo is expected.
You can make name a property, and implement a setter that updates the list.
class Person:
list = []
def __init__(self, name="Zed"):
self._name = name
self.list.append(name)
#property
def name(self):
return self._name
#name.setter
def name(self, name):
if self._name in self.list:
# remove the old name
index = self.list.index(self.name)
self.list[index] = name
else:
self.list.append(name)
self._name = name
def printList(self):
print(self.list)
a = Person()
a.name = "Yasuo"
a.printList() # prints ['Yasuo']

Python: How to declare an object of class from another object which is also variable in this class?

class Apple:
def print_my_tree_name(self):
print('I want to use name of Tree on which I\'m hanging')
class Tree:
def __init__(self, name):
self.name = name
self.apple = Apple()
a = Tree('test')
a.apple.print_my_tree_name()
Is it possible to access the name variable without passing self to the Apple class e.g. a.apple.print_my_tree_name()?
You can specify the name of the tree as an attribute of the apple in the tree constructor
class Apple:
def print_my_tree_name(self):
print('I am hanging on tree: %s'%self.tree_name)
class Tree:
def __init__(self, name):
self.name = name
self.apple = Apple()
self.apple.tree_name = name
a = Tree('test')
a.apple.print_my_tree_name()
Perhaps something like this, using a #staticmethod
class Tree:
def __init__(self, name):
self.name = name
self.apple = Apple()
class Apple():
#staticmethod
def print_my_tree_name():
print(f'Hanging on {a.name} tree.')
a = Tree('test')
a.apple.print_my_tree_name()
# Hanging on test tree.
I am not sure why you want to access name in Apple class but if I had to do this, I will implement it something as below
class Tree:
def __init__(self, name):
self.name = name
class Apple:
def __init__(self, name):
self.tree = Tree(name)
def print_my_tree_name(self):
print('I want to use %s'%self.tree.name)
a = Tree('test')
a.print_my_tree_name()
See composition in python that is what you need basically.
http://blog.thedigitalcatonline.com/blog/2014/08/20/python-3-oop-part-3-delegation-composition-and-inheritance/

Printing a list from a class in Python

My simplified code is below: it creates an animal, and places it inside a zoo. I want to print the list of animals within the zoo. Going round in circles with this!
class Animal(object):
def __init__(self, name):
self.name = name
class Zoo(object):
def __init__(self):
self.animals = []
def __str__(self):
rep = ", ".join(self.animals)
return rep
def add(self, name):
self.animals.append(Animal(name))
def main():
while True:
zoo = Zoo()
animal = input("add an animal: ")
zoo.add(animal)
print(zoo)
main()
The added __repr__ Method to the Animal returns us the name.
The zoo = Zoo() has to be outside of the loop, this makes sure that we do not create a new zoo with every iteration.
Then we print the list (zoo.animals).
class Animal(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
class Zoo(object):
def __init__(self):
self.animals = []
def __str__(self):
rep = ", ".join(self.animals)
return rep
def add(self, name):
self.animals.append(Animal(name))
def main():
zoo = Zoo()
while True:
animal = input("add an animal: ")
zoo.add(animal)
print(zoo.animals)
main()
You can simply refer to the name property of Animal in your Zoo.__str__(), e.g.:
def __str__(self):
return ', '.join(animal.name for animal in self.animals)
Now print(zoo) should work correctly.
However this doesn't provide a lot of encapsulation if say you wanted to change what it means to print an animal, e.g. height, size, etc. So perhaps a more encapsulated form would be:
class Animal(object):
...
def __str__(self):
return self.name
class Zoo(object):
...
def __str__(self):
return ", ".join(str(animal) for animal in self.animals)
Now when you print(zoo) the Animal class is responsible for its own string presentation.
Just as a note: you probably should create the Animal instance outside of Zoo, what happens if you decide to create a class hierarchy of Animals (e.g. Mammal) that has different behaviours, your Zoo class would only know about Animals.
class Animal(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
class Zoo(object):
def __init__(self):
self.animals = []
def __str__(self):
return ", ".join(str(animal) for animal in self.animals)
def add(self, animal):
self.animals.append(animal)
def main():
zoo = Zoo()
while True:
animal = Animal(input("add an animal: "))
zoo.add(animal)
print(zoo)
main()
This would still behave properly if you create a Mammal class:
class Mammal(Animal):
...
zoo.add(Mammal(...))
print(zoo)

Python - Class methods with input

How can I create a method in a class with a user input?
What argument should I pass when I am calling the method ?
class Student:
def __init__(self):
self._name = ''
def getName(self):
return self._name
def setName(self, newName):
newName = input ('Inserire nome:')
self._name = newName
studente = Student()
studente.setName(newName)
This should work:
class Student:
def __init__(self):
self._name = ''
def getName(self):
return self._name
def setName(self, newName):
self._name = newName
studente = Student()
newName = input('Inserire nome:')
studente.setName(newName)
You were defining the input inside the method itself but passing the variable outside. So the variable newName wasn't defined outside. Let me know if it doesn't work. I haven't tested it, but seems like the conspicuous error here.
If I understood what you want correctly,
why dont you try to ask for an input when initialsing class instance?
class MyClass(object):
def __init__(self):
self.name = input('Enter name> ')
X = MyClass() # when executing that line, you'll be asked for a name to input
Then you'll be able to acces name attribute by X.name and set it to whatever you'd like to by X.name = foo
You could also play with the builtin setattr and dismiss the explicit setters/getters:
class Student:
pass
student = Student()
newName = input('Inserire nome:')
setattr(student, 'name', newName)

Initialise child class with instance of parent class

Suppose I have a class:
class Person(object):
def __init__(self, name, hobbies):
self.name = name
self.hobbies = hobbies
... (and so on)
Now I want to initialise a child class, Employee, which extends person. I would like to initialise that class with an instance of the Person class. So I would like to do:
class Employee(Person):
def __init__(self, person, salary):
# Initialise the superclass from the given instance somehow
# I know I could do:
super(Employee, self).__init__(person.name, person.hobbies)
# But could I somehow do something like:
super(Employee, self).__init__(person)
# (In this case the difference is small, but it could
# be important in other cases)
# Add fields specific to an "Employee"
self.salary = salary
So that I can then call:
p1 = Person('Bob', ['Bowling', 'Skiing'])
employed_p1 = Employee(p1, 1000)
Is there any way I can do this, or do I explicitly have to call the parent class's constructor again?
Thanks a lot!
I thnk you want something like this:
class Person(object):
def __init__(self, name, hobbies):
self.name = name
self.hobbies = hobbies
def display(self):
print(self.name+' '+self.hobbies[0])
class Employee(Person):
def __init__(self, a, b =None,salary=None):
if b is None:
self.person = a
else:
self.person = Person(a,b)
self.name = self.person.name
self.hobbies = self.person.hobbies
self.salary = salary
bob = Employee('bob',['Bowling', 'Skiing'])
bob.display()
sue1 = Person('sue',['photography','music'])
sue2 = Employee(sue1,salary=123)
sue2.display()
I've added in the 'display' function just to make it easier to follow. Hope this helps.

Categories