Python, involving defining a class [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have this problem I am trying to solve:
Write the definition of a class ContestResult containing:
•An instance variable winner of type String , initialized to the empty String.
•An instance variable second_place of type String , initialized to the empty String.
•An instance variable third_place of type String , initialized to the empty String.
•A method called set_winner that has one parameter, whose value it assigns to the instance variable winner .
•A method called set_second_place that has one parameter, whose value it assigns to the instance variable second_place .
•A method called set_third_place that has one parameter, whose value it assigns to the instance variable third_place .
•A method called get_winner that has no parameters and that returns the value of the instance variable winner .
•A method called get_second_place that has no parameters and that returns the value of the instance variable second_place .
•A method called get_third_place that has no parameters and that returns the value of the instance variable third_place .
This is my code:
class ContestResult():
def __init__(self):
self.winner= ""
self.second_place= ""
self.third_place= ""
def set_winner(self,value):
self.winner= value.set_winner
def set_second_place(self,value):
self.second_place= value_set_second_place
def set_third_place(self,value):
self.third_place= value.set_third_place
def get_winner(self):
return self.winner
def get_second_place(self):
return self.second_place
def get_third_place(self):
return self.third_place
I am receiving this error:
Exception occurred(<class 'AttributeError'>, AttributeError("'str' object has no attribute 'set_winner'",)
Can someone help me with this? Please and thank you.

One are passing in a string to set_winner. That is why you're seeing the error.
However, you don't need to create the set_winner and get_winner functions in Python. All of the object's member variables, in this case winner, second_place and third_place, are publicly accessible. If you really want to have mutators and accessors, the Pythonic way of doing so is with property:
class ContestResult(object):
def __init__(self):
self.__winner = ''
#property
def winner(self):
return self.__winner
#winner.setter
def winner(self, value):
print "I'm about to set the value of winner"
self.__winner = value
and now when you want to see the "winner" property:
foo = ContestResult()
foo.winner = "Frank"
it works just like a regular member variable.

in def set_winner(self,value), presumably value is a string, right? Why are you calling value.set_winner?
self.winner = value
Note that this is true for all your set_s.

You really don't need all that with Python unless you're trying to modify the way attributes are actually get and set.
class ContestResult(object):
def __init__(self, winner, second_place, third_place):
self.winner = winner
self.second_place = second_place
self.third_place = third_place
Stick = ContestResult("Foo", "Bar", "Eggs")
Stick.second_place #gets "Bar"
Stick.winner = "Clown" #sets Stick.winner
Python isn't Java :) you don't need those methods -- again, unless you're going to be modifying the data or decorate the method or something. In which case you can use the #property syntax as outlined by wheaties -- and in doing so, you still don't actually call the get or set methods, you just interact in a Pythonic way and use assignment.
Stick.second_place = "Earl" #this will call the property setter

Related

How to use any obtained variable from a function in other functions in Python classes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 21 days ago.
Improve this question
I am trying to use one variable obtained from one function in other function. However , it gives error. Let me explain it wih my code.
class Uygulama(object):
def __init__(self):
self.araclar()
self.refresh()
self.gateway_find()
def refresh(self):
self.a, self.b = srp(Ether(dst="FF:FF:FF:FF:FF:FF") / ARP(pdst=self.ip_range2), timeout=2, iface="eth0",
retry=3)
#There are unrelated codes here
def gateway_find(self):
#Find ip any range in which you conncet:
self.ip_range=conf.route.route("0.0.0.0")[1]
self.ip_range1=self.ip_range.rpartition(".")[0]
self.ip_range2=self.iprange_1+".0/24"
When , run the foregoing codes , i get this error AttributeError: 'Uygulama' object has no attribute 'ip_range2'
How can i use such variable which are obtained from other function in the other function. How can i fix my problem ?
Call order of init functions
Place function that define attribute first
In the __init__ function, you call refresh, who use (need) ip_range2 before gateway_find who create the attribute and set a value to it. Swap the two lines, you should be fine.
def __init__(self):
self.araclar()
self.gateway_find() # gateway_find will set the ip_range attribute
self.refresh() # So refresh function will be able to access it
Usually, we place init functions first, then function that will call post-init processes like refresh.
Class attribute default value
Alternatively, you can define a default value for ip_range2 like this:
class Uygulama(object):
ip_range2 = None
def __init__(self):
self.araclar()
self.refresh()
self.gateway_find()
def refresh(self):
self.a, self.b = srp(Ether(dst="FF:FF:FF:FF:FF:FF") / ARP(pdst=self.ip_range2), timeout=2, iface="eth0", retry=3)
Be aware that such default value is shared with all other instances of the class if not redefined in __init__, so if it's a mutable (like a list), it might create really weird bugs.
Usually, prefer defining value in the __init__ like you do with the gateway fct.
That error explains correctly that you do not have a class attribute called ip_range2. You need to define the class attribute first.
class Uygulama(object):
ip_range2 = ''
...
then use that with self.ip_range2.

Using class variables in methods of the same class in Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am currently learning Python at home using the Python for Dummies all in one book. I'm on the chapter about classes and in particular the section on class variables. So far, the code has been running fine, but I'm not sure why it doesn't work the way I expect.
My code is this:
class Crew_Member:
"""A class for creating an individual record"""
is_human = True
def __init__(self, full_name, username, rank="crewmember"):
self.full_name = full_name
self.username = username
self.rank = rank
self.join_date = dt.date.today()
def file_age(self):
return f"{self.full_name}'s file was created on {self.join_date:%d/%m/%y}"
def promote(self, rank):
self.rank = rank
def not_human(self):
if Crew_Member.is_human:
self.rank = "Mech/Scutter"
So my understanding is that is_human is the class variable. The method I'm trying to use it in is not_human. The idea is that each object would be the personel record for a crew member. If that crew member is not human, they automatically get the rank of "Mech/Scutter".
The way I imagined it working is after the object has been called, you change that instance's value of is_human to false, run the method not_human, and that would change their rank accordingly. The first time I tried and got the correct rank, the class variable hadn't changed:
My code (as written above) works fine, but this is what I have to enter to get it to work:
So this is my problem: The for (edit: if) loop in the not_human method says "If class variable is true, then change rank". But the class variable has been changed to false (as illustrated by the first print line) so why does it work?
But the class variable has been changed to false...
No, the line BobScutt.is_human = False turns it into an instance variable. The class variable remains untouched. If you want to change it, you have to manipulate the Crew_Member class directly, not one of its instances.
Observe:
class TestClass:
test_attr = True
foo = TestClass()
foo.test_attr = False
print("Test attribute in foo:", foo.test_attr)
print("Test attribute in TestClass:", TestClass.test_attr)
Output:
Test attribute in foo: False
Test attribute in TestClass: True

Python Function that creates class instances

Hello i want to create a function which creates instances of a class
def make_instance(name_instance)
name_instance=puppy()
class puppy:
def __init__(self,name):
self.name =name
make_instance(cloud)
# when i pass an argument it says the variable is undefined and i use #isinstance() it return False.
Your puppy class needs to take a name value into its constructor, and you're currently not passing in anything.
Also your function doesn't return the instance at all. It simply re-assigns the instance to the variable name_instance that you pass in (losing your input). The return value of make_instance right now is None
My guess is that you want your implementation to look like the following
def make_instance(name_instance)
return puppy(name_instance)
I do want to point out though that this function isn't useful unless it does more than just create the instance, you're just adding wrapper code around the constructor

Class variable and Instance variable in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am practicing python classes. I got that classes variables are shared among all the instances, while instance variables belongs to each object, and thus need to be defined for each instance. In the following classes, in reference to the variable raise_amount, if I write the last code line like that: self.pay = int(self.pay * Employee.raise_amount), the behavior is the same.
What is the difference between the two cases, if any?
class Employee:
raise_amount = 1.04
def __init__(self ,first ,last ,pay ):
self.first = first
self.last = last
self.pay = pay
self.email = first+"."+last+"#company.com"
def apply_raise(self): #but this is not an attribute
self.pay = int(self.pay * self.raise_amount)
#or self.pay = int(self.pay * Employee.raise_amount)
I assume your are referring to the self.raise_amount. In the case where you have it as that python must first look for instance variable and if not found it looks for similarly named class variable and creates a copy of it as an instance variable and uses that. if you were to then change the self.raise amount it would only be for the instance and not for any other instance made from the class.
Try creating an instance of the class and use the apply_raise method on it. then try changing the raise_amount class variable to something like 2.0 and call the method on the instance again. what you should see is that the amount only goes up 1.04 times, not 2. that is because it used the instance value it created the first time you ran the method.
Note: original post was edited to change to use the Class name for the raise amount. This post was a reply while it was self.raise_amount.
The lookup for the expression self.raise_amount can be complicated in general, be we can simplify it knowing that there are no methods or descriptors involved.
If self has an instance attribute named raise_amount, the value of that attribute is returned.
Otherwise, we start looking for class attributes, starting with the immediate type of self, here Employee. Since Employee.raise_amount is defined, we get that value...
... but what if Employee.raise_amount hadn't been defined? We would have moved on to the next class in the method resolution order of Employee, namely object. Since object.raise_amount is not defined, an AttributeError would have been raised.

Clarification regarding self [duplicate]

This question already has answers here:
When is "self" required?
(3 answers)
Closed 8 years ago.
In methods when is it necessary to use notation like self.variable_name? For instance, I know that in the constructor method it needs to be like
class A(object):
def __init__(self, name):
self.name = name
in order to give it an instance variable. However, what about in other methods? When do I need to put self in front of a variable name and when is it okay to just use a variable name?
You must always put a self as the first argument of an instance method. This is what you use to access the instance variables.
It is similar to this in other languages, but different in that it is required whereas other languages will often have instance variables in scope.
Whenever you are wanting to access attributes of the particular object of type A. For example:
def get_name(self): # here, whenever this method is called, it expects 'self'.
return self.name
When calling this method outside of the class scope, python has self passed implicitly.
example = A('d_rez')
example.get_name() # no arguments are passed, but self is passed implicitly to
# have access to all its attributes!
So to answer your question: You need to pass self whenever you define a method inside a class, and this is usually because there are some attributes involved. Otherwise, it could just be a static function defined outside the class if it doesn't pertain to a particular instance of class A.

Categories

Resources