python


Python, class initialization with one value


I have a class which takes two numbers and prints them in a list
class Numbers():
def __init__(self, l, r):
self.l = l
self.r = r
def __str__(self):
print([self.l, self.r])
ex:
N = Numbers(1, 3) #[1, 3]
Now, I need to extend the class so that it can be initialized with only one value
N2 = Numbers(2) #[2, 2]
Right now I'm a bit clueless on how to proceed, any help is appreciated
You can simply use a default parameter:
class Numbers():
def __init__(self, l, r=None):
self.l = l
if r is None:
self.r = l
else:
self.r = r
def __str__(self):
return str([self.l, self.r])
print(Numbers(1, 2))
# [1, 2]
print(Numbers(3))
# [3, 3]
How about this:
def __init__(self, l, r = None):
self.l = l
if r is None:
self.r = l
else:
self.r = r
As others already pointed out, you'll need to modify your __str__ method as well, to be something like this:
def __str__(self):
return str(self.l) + "," + str(self.r)
Try this:
class Numbers:
def __init__(self, l, r=None):
self.l = l
self.r = r if r else l
def __str__(self):
return str([self.l, self.r])
ex:
n1 = Numbers(3, 5)
print(n1) # [3, 5]
n2 = Numbers(2)
print(n2) # [2, 2]

Related Links

Computing the gradient for a custom TensorFlow op with py_func()
get all files in drive via REST API
Setting an image as a tkinter window background
Displaying A File Using StringVar() // Tkinter Python 3.5
How are features ranked in RFECV in scikit learn(sklearn)?
Solr requests hang when started via python subprocess
How to extract internet email headers from outlook emails?
Tensorboard get blank page
Python Matplotlib Streamplot providing start points
Reduce inner points in numpy coordinate dataset (speed up concave hull)
Blitting several layers pygame
floating and integer power difference in Python
how to skip certain line in text file and keep reading the next line in python?
match key and insert into new column
Python - Storing the values of CSV file in List
Should telegram-code be defined as a message option?

Categories

HOME
visual-studio-2015
sas
symfony
payment-gateway
xamarin.android
drupal-7
system-verilog
warnings
ocaml
steam
mapbox-gl-js
safari
javamail
laravel-4
adsense
xtext
gimp
ssr
uiview
jquery-terminal
pyephem
jpeg2000
styles
jni
plsqldeveloper
spring-ws
go-cd
fabric
opencart2.3
kong
vcard
mayavi
thinking-sphinx
smart-mobile-studio
spring-profiles
construct-2
large-data
coreclr
image-compression
mongoexport
topic-modeling
equivalence
jdbi
resuming-training
amd
matlab-cvst
asmx
appstore-approval
mbaas
in-memory-database
unity3d-editor
spring-ioc
gitweb
multifile-uploader
windows-nt
django-south
asp.net-mvc-partialview
axes
acm
difference
drf-nested-routers
file-diffs
optionbutton
paypal-nvp
sparse-file
subversion-edge
code-first-migrations
virus
sqoop2
hana-xs
pgagent
gyroscope-framework
getimagedata
adaptive-compression
mfmailcomposeviewcontroll
slick-2.0
java.util.date
convex-polygon
sslexception
symphony-cms
digital-design
awesomeprint
wpdb
cadisplaylink
unions
text-services-framework
nssavepanel
tcxgrid
datacontract
easygui
opengl-es-lighting
nvelocity
getresource
external-assemblies
internals
interface-design

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