python


When defining a python class, how to set a random variable in it?


Say I have a class called Person, which will have only the person's name and gender.
The gender should be randomly selected from Male and Female. To do that, I import the random.randint() function. The random gender is determined according to the random int.
import random
class Person:
alias = random.randint(1, 3)
if alias == 2:
gender = 'Male'
else:
gender = 'Female'
def __init__(self, name):
self.name = name
r = Person('rachel')
s = Person('Stanky')
print(r.gender)
print(s.gender)
However, the result I get for different person from this class all have the same gender. My understanding is the randint is fixed once been generated. My question is how to make it different for each class instance.
The reason your never getting a different gender is because the gender attribute is a class variable. Once Python creates your class object Person, the value of gender is never computed again. You need to make the gender variable an instance attribute. That means the gender will be computed on a per instance basis, instead of per class definition. eg:
import random
class Person:
def __init__(self, name):
self.name = name
alias= random.randint(1, 3)
if alias == 2:
self.gender = 'Male'
else:
self.gender = 'Female'
r= Person('rachel')
s= Person('Stanky')
print(r.gender)
print(s.gender)
Which outputs
Female
Male
On a side note, I think a much better choice for your task would be to use random.choice() to select a random gender from a list of genders(male or female):
from random import choice
class Person:
genders = ('Male', 'Female', 'Female')
def __init__(self, name):
self.name = name
self.gender = choice(Person.genders)
rachel = Person('Rachel')
stanky = Person('Stanky')
print(rachel.gender)
print(stanky.gender)
Notice in my example above, Person.genders is a class variable. This is on purpose. Because we only need to create a list of genders once, we can make the list a class level variable.
It seems you already guessed the reason correctly why the gender doesn't change. To solve this you could use a property for gender:
import random
class Person(object):
def __init__(self, name):
self.name = name
#property
def gender(self):
try:
# If it has been defined for the instance, simply return the gender
return self._gender
except AttributeError:
# If it's not defined yet, define it, and then return it
self._gender = random.choice(['male', 'female'])
return self._gender
r = Person('rachel')
s = Person('Stanky')
print(r.gender)
print(s.gender)
You should put random gender generation under __init__, so that it would be randomly generated for each class instance.
import random
class Person:
def __init__(self, name):
self.name = name
alias= random.randint(1, 3)
if alias == 2:
self.gender = 'Male'
else:
self.gender = 'Female'
r= Person('rachel')
s= Person('Stanky')
print(r.gender)
print(s.gender)
Three times of results:
$ python classRandom.py
Male
Female
$ python classRandom.py
Female
Female
$ python classRandom.py
Female
Male
The gender variable is currently a class variable, that's why it stays the same for all the instance created. You will need to change them into a instance's attribute.
Side note: random.randint(1, 3) will either return 1, 2 or 3 not just 1 or 2. It is not the same as indexing, in which the ending is ignored. Right now, there's a higher chance to be a Female since it could be 1 or 3, so random.randint(1,2) makes more sense. I mentioned this just in case it was not done on purpose. You can also use random.choice, which will choose a random selection from a list.
import random
class Person:
def __init__(self, name):
self.name = name
self.gender= random.choice(["Male", "Female"]) # or random.choice(["Male", "Female", "Female"]) if you want a higher chance of being a female
r= Person('rachel')
s= Person('Stanky')
print(r.gender)
print(s.gender)

Related Links

Trigger a delete when inserting a new entry on django
How does gauss laguerre integration works for large limits?
Selenium: Runtime.executionContextCreated has invalid 'context':
Python 3.5 csv input shows extra character 'b'
Python Error 104, connection reset by peer
css not getting loaded in flask webpage?
Regarding the regex in search module with and without raw text
How to calculate the mean of a column by decade in Python
How to read webpages that are without .htm* extension using Python?
can I use python's 'socket' module to connect to a wireless ethernet host?
Requests VS Urllib 2 [closed]
Call python class from another Python script
How can I use BeautifulSoup to get a few contents that comes after a specific text on a page?
Reinstall python 2.7.12 and python 3.5.2
How to keep chrome browser window open after selenium script finishes on python
Outlook email attachment downloader (Date range)

Categories

HOME
app-inventor
openssl
windows-7
backup
angular2-directives
chaiscript
safari
rocketmq
dataframe
sudo
slim-3
x11
rfid
xul
uiscrollview
image-recognition
data-synchronization
abcpdf
symfony2-easyadmin
jacoco
aptana
chocolatey
flexlm
cruisecontrol.net
cocoa-touch
jquery-scrollify
tflearn
interrupt-handling
pptp
system.data.sqlite
mangodb
bitcoin-testnet
datadog
referenceerror
backup-strategies
fractals
threshold
nlb
dql
soundjs
lexical-analysis
mediawiki-extensions
tripwire
constants
finite-element-analysis
rkt
worker
nsjsonserialization
pinvoke
mouseclick-event
jgraph
ogre3d
butterknife
in-memory-database
laravel-query-builder
inject
coremidi
easing
rstudio-server
amf
spinlock
amazon-kcl
asp.net-mvc-2
servlet-3.0
cctray
intellitest
zuora
sevenzipsharp
twython
ubercart
ibm-data-studio
ember-cli-addons
google-earth-plugin
jcr-sql2
c++-actor-framework
cpu-speed
left-recursion
node-inspector
yahoo-boss-api
bsp
korma
level
slick-2.0
uipickerviewcontroller
trimming
ng-hide
cgimageref
qt4.6
process-monitor
ubuntu-11.10
javascriptserializer
sunspot-rails
cosm
macruby
z-machine
gjs
shared-objects
webkit-transform
executable-format
paintcomponent
rubycas
collect
loadui
gwt-2.2-celltable
longjmp
nt4

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App