Python

sum

why

not

strings

closed

Ask

Question

Python

has

a

built

in

function

sum

which

is

effectively

equivalent

to

def

iterable

start

return

start

reduce

operator

add

iterable

for

all

types

of

parameters

except

strings

It

works

for

numbers

and

lists

for

example

sum

Note

is

the

default

value

for

start

but

I

include

it

for

clarity

sum

Why

were

strings

specially

left

out

sum

foo

bar

TypeError

sum

cant

sum

strings

use

joinseq

instead

foo

bar

foobar

I

seem

to

remember

discussions

in

the

Python

list

for

the

reason

so

an

explanation

or

a

link

to

a

thread

explaining

it

would

be

fine

I

am

aware

that

the

standard

way

is

to

do

join

My

question

is

why

the

option

of

using

sum

for

strings

was

banned

and

no

banning

was

there

for

say

lists

Edit

Although

I

believe

this

is

not

needed

given

all

the

good

answers

I

got

the

question

is

Why

does

sum

work

on

an

iterable

containing

numbers

or

an

iterable

containing

lists

but

not

an

iterable

containing

strings

python

types

sum

builtin

edited

Dec

at

Yvette

Colomb

asked

Aug

at

Muhammad

Alkarouri

closed

as

primarily

opinionbased

by

TylerH

ekad

Yvette

Colomb

Dec

at

Many

good

questions

generate

some

degree

of

opinion

based

on

expert

experience

but

answers

to

this

question

will

tend

to

be

almost

entirely

based

on

opinions

rather

than

facts

references

or

specific

expertise

If

this

question

can

be

reworded

to

fit

the

rules

in

the

help

center

please

edit

the

question

locked

by

Yvette

Colomb

Dec

at

This

question

exists

because

it

has

historical

significance

but

it

is

not

considered

a

good

ontopic

question

for

this

site

so

please

do

not

use

it

as

evidence

that

you

can

ask

similar

questions

here

This

question

and

its

answers

are

frozen

and

can

not

be

changed

More

info

help

center

NullUserException

it

makes

as

much

sense

to

sum

strings

as

it

is

to

sum

lists

Muhammad

Alkarouri

Aug

at

NullUserException

It

would

be

great

if

you

were

right

but

sadly

the

operation

on

strings

is

already

overloaded

to

mean

concatentation

So

with

we

already

construct

string

sums

Aug

at

SLott

I

meant

summing

a

sequence

of

lists

compared

to

summing

a

sequence

of

strings

As

it

happens

sum

of

a

list

of

lists

concatenates

the

lists

You

can

sum

two

lists

using

to

concatenate

them

You

can

sum

two

strings

using

to

concatenate

them

So

it

makes

as

much

sense

to

define

sum

as

concatenation

for

strings

as

it

is

for

lists

That

is

what

I

meant

Whether

this

is

good

or

is

bad

is

beside

the

question

Muhammad

Alkarouri

Aug

at

SLott

read

my

question

again

It

is

quite

clear

there

I

said

for

all

types

of

parameters

except

strings

It

works

for

numbers

and

lists

for

example

Which

means

that

numbers

and

lists

are

parameters

in

much

the

same

way

strings

are

How

did

you

understand

the

comparison

between

sum

and

join

Muhammad

Alkarouri

Aug

at

SLott

Not

to

beat

a

dead

horse

but

I

read

the

question

and

understood

it

instantly

And

on

a

more

technical

level

characters

in

a

Python

string

are

just

strings

themselves

you

can

technically

can

sum

the

characters

resulting

in

ordinary

concatenation

joinfoo

for

example

returns

foo

javawizard

Dec

at

comments

disabled

on

deleted

locked

posts

reviews

show

more

comments

Answers

active

oldest

votes

Python

tries

to

discourage

you

from

summing

strings

Youre

supposed

to

join

them

Its

a

lot

faster

and

uses

much

less

memory

A

quick

benchmark

python

m

timeit

s

import

operator

strings

r

reduceoperatoradd

strings

loops

best

of

msec

per

loop

python

m

timeit

s

import

operator

strings

r

joinstrings

loops

best

of

usec

per

loop

Edit

to

answer

OPs

edit

As

to

why

strings

were

apparently

singled

out

I

believe

its

simply

a

matter

of

optimizing

for

a

common

case

as

well

as

of

enforcing

best

practice

you

can

join

strings

much

faster

with

join

so

explicitly

forbidding

strings

on

sum

will

point

this

out

to

newbies

BTW

this

restriction

has

been

in

place

forever

ie

since

the

sum

was

added

as

a

builtin

function

rev

edited

Aug

at

answered

Aug

at

rbp

Actually

I

think

its

the

other

way

around

reduce

and

sum

are

similar

for

lists

because

they

do

more

or

less

the

same

thing

Thats

why

I

used

this

benchmark

reduce

representing

sum

versus

join

which

is

an

optimised

way

of

adding

strings

rbp

Aug

at

I

am

feeling

old

now

I

understand

forever

in

Python

as

before

Things

done

after

the

introduction

of

new

style

classes

are

not

that

forever

Muhammad

Alkarouri

Aug

at

It

does

seem

unpythonic

to

discourage

this

usage

I

agree

with

Muhammad

that

its

a

strange

exception

But

it

seems

this

is

the

best

reason

available

Edmund

Aug

at

I

think

its

there

because

of

the

chronological

sequence

of

implementations

when

they

implemented

the

sum

builtin

they

already

had

join

for

strings

So

to

avoid

people

unknowingly

or

knowingly

but

lazilymischievously

using

sum

for

strings

they

specifically

disallowed

it

Since

there

wasnt

a

specific

sum

for

lists

or

other

types

they

made

the

exception

only

for

strings

Nowadays

I

think

theyd

keep

it

this

way

even

if

someone

came

up

with

a

specific

sum

for

backwards

compatibility

rbp

Aug

at

Its

not

optimizing

for

a

common

case

Optimization

would

be

using

that

same

if

statement

blocking

it

to

instead

invoke

the

already

written

strjoin

code

Since

that

would

be

such

an

easy

optimization

to

make

Id

assume

the

intent

is

to

deliberate

fail

noisily

to

teach

people

about

the

existence

of

strjoin

Ben

Apr

at

show

more

comments

You

can

in

fact

use

sum

to

concatenate

strings

if

you

use

the

appropriate

starting

object

Of

course

if

you

go

this

far

you

have

already

understood

enough

to

use

join

anyway

class

ZeroObject

object

def

self

other

return

other

sum

hi

there

ZeroObject

hithere

answered

Aug

at

I

find

this

very

interesting

Of

course

it

is

too

clever

by

half

but

it

adds

to

the

understanding

of

this

feature

Muhammad

Alkarouri

Aug

at

And

its

a

little

faster

than

the

reduce

version

Debilski

Aug

at

Still

its

weird

that

Python

checks

for

strings

but

not

for

lists

or

tuples

Philipp

Aug

at

add

a

comment

Heres

the

source

In

the

function

we

have

this

bit

of

code

reject

string

values

for

start

parameter

if

result

sum

cant

sum

strings

use

joinseq

instead

iter

return

result

So

thats

your

answer

Its

explicitly

checked

in

the

code

and

rejected

answered

Aug

at

HS

Its

interesting

to

see

the

code

but

the

question

was

Why

arent

strings

summed

not

How

did

they

exclude

strings

from

summing

Edmund

Aug

at

Well

I

meant

the

reason

why

its

not

working

is

because

its

explicitly

banned

in

the

code

Others

seemed

to

answer

by

explaining

why

you

shouldnt

do

it

HS

Aug

at

Thanks

for

sharing

the

source

code

But

I

get

a

different

traceback

when

I

try

to

sum

two

strings

sumfoo

bar

TypeError

unsupported

operand

types

for

int

and

str

But

sumfoo

bar

TypeError

sum

cant

sum

strings

use

joinseq

instead

Any

ideas

why

the

first

one

returns

a

different

traceback

I

think

its

more

likely

to

occur

that

way

Bill

Oct

at

I

just

noticed

the

answer

to

my

above

comment

is

explained

by

below

I

now

realise

the

argument

is

the

initial

value

for

the

summation

process

Bill

Oct

at

add

a

comment

From

the

docs

The

preferred

fast

way

to

concatenate

a

sequence

of

strings

is

by

calling

joinsequence

By

making

sum

refuse

to

operate

on

strings

Python

has

encouraged

you

to

use

the

correct

method

edited

Feb

at

answered

Aug

at

unutbu

So

its

part

of

the

one

way

to

do

it

philosophy

As

they

could

just

have

made

sum

treat

strings

specially

Thomas

Ahle

Feb

at

ThomasAhle

There

is

a

computational

reason

why

sum

is

the

wrong

tool

for

the

job

sum

builds

the

result

incrementally

Doing

so

with

strings

requires

potentially

many

temporary

immutable

strings

If

done

naively

this

process

requires

lots

of

temporary

memory

and

lots

of

copying

In

contrast

if

done

with

join

the

appropriate

amount

of

memory

is

allocated

from

the

beginning

and

result

is

built

right

there

at

once

See

Martellis

exhortation

unutbu

Feb

at

Sure

but

you

can

do

lots

of

inefficient

things

in

Python

and

often

it

doesnt

matter

I

cant

think

of

any

other

cases

than

sum

of

strings

where

there

are

tests

against

doing

something

hard

coded

in

the

Python

C

code

Thomas

Ahle

Feb

at

add

a

comment

Short

answer

Efficiency

Long

answer

The

sum

function

has

to

create

an

object

for

each

partial

sum

Assume

that

the

amount

of

time

required

to

create

an

object

is

directly

proportional

to

the

size

of

its

data

Let

N

denote

the

number

of

elements

in

the

sequence

to

sum

double

s

are

always

the

same

size

which

makes

sum

s

running

time

ON

int

formerly

known

as

long

is

arbitarylength

Let

M

denote

the

absolute

value

of

the

largest

sequence

element

Then

sum

s

worstcase

running

time

is

lgM

lgNM

NlgM

lgN

ON

log

N

For

str

where

M

the

length

of

the

longest

string

the

worstcase

running

time

is

M

NM

N

ON

Thus

sum

ming

strings

would

be

much

slower

than

sum

ming

numbers

strjoin

does

not

allocate

any

intermediate

objects

It

preallocates

a

buffer

large

enough

to

hold

the

joined

strings

and

copies

the

string

data

It

runs

in

ON

time

much

faster

than

sum

answered

Aug

at

That

argument

is

wrong

because

the

same

would

hold

for

lists

and

tuples

which

can

be

summed

As

stated

by

HS

Python

explicitly

check

for

strings

and

only

for

strings

which

just

doesnt

make

sense

Philipp

Aug

at

Philipp

The

missing

piece

is

that

many

many

people

were

trying

to

use

sum

for

strings

and

not

many

use

sum

for

lists

and

tuples

The

trap

is

that

sum

works

just

fine

for

short

lists

of

strings

but

then

gets

put

in

production

where

the

lists

can

be

huge

and

the

performance

slows

to

a

crawl

This

was

such

a

common

trap

that

the

decision

was

made

to

ignore

ducktyping

in

this

instance

and

not

allow

strings

to

be

used

with

sum

Ethan

Furman

Apr

at

Excellent

answer

Except

for

would

be

much

slower

has

to

be

is

much

slower

as

has

been

proven

steffen

Nov

at

add

a

comment

The

Reason

Why

has

an

excellent

explanation

for

the

costs

of

using

sum

on

large

lists

of

strings

The

missing

piece

as

to

why

str

is

not

allowed

for

sum

is

that

many

many

people

were

trying

to

use

sum

for

strings

and

not

many

use

sum

for

lists

and

tuples

and

other

data

structures

The

trap

is

that

sum

works

just

fine

for

short

lists

of

strings

but

then

gets

put

in

production

where

the

lists

can

be

huge

and

the

performance

slows

to

a

crawl

This

was

such

a

common

trap

that

the

decision

was

made

to

ignore

ducktyping

in

this

instance

and

not

allow

strings

to

be

used

with

sum

answered

Apr

at

Ethan

Furman

Is

there

any

reason

why

the

sum

implementation

doesnt

just

call

join

when

a

string

is

encountered

in

sum

instead

of

returning

an

error

instructing

you

to

call

join

Slater

Apr

at

Slater

Yes

But

I

dont

recall

what

it

was

Ethan

Furman

Apr

at

add

a

comment

Edit

Moved

the

parts

about

immutability

to

history

Basically

its

a

question

of

preallocation

When

you

use

a

statement

such

as

sum

a

b

c

and

expect

it

to

work

similar

to

a

reduce

statement

the

code

generated

looks

something

like

a

must

allocate

and

set

its

size

to

len

lena

b

must

allocate

and

set

its

size

to

lena

lenb

res

must

allocate

res

and

set

its

size

to

len

In

each

of

these

steps

a

new

string

is

created

which

for

one

might

give

some

copying

overhead

as

the

strings

are

getting

longer

and

longer

But

thats

maybe

not

the

point

here

Whats

more

important

is

that

every

new

string

on

each

line

must

be

allocated

to

its

specific

size

which

I

dont

know

it

it

must

allocate

in

every

iteration

of

the

reduce

statement

there

might

be

some

obvious

heuristics

to

use

and

Python

might

allocate

a

bit

more

here

and

there

for

reuse

but

at

several

points

the

new

string

will

be

large

enough

that

this

wont

help

anymore

and

Python

must

allocate

again

which

is

rather

expensive

A

dedicated

method

like

join

however

has

the

job

to

figure

out

the

real

size

of

the

string

before

it

starts

and

would

therefore

in

theory

only

allocate

once

at

the

beginning

and

then

just

fill

that

new

string

which

is

much

cheaper

than

the

other

solution

edited

Aug

at

answered

Aug

at

Debilski

Its

true

but

thats

not

the

whole

story

Integers

are

immutable

as

well

But

the

join

operation

on

strings

is

specialised

takes

the

whole

list

into

account

and

therefore

is

much

faster

rbp

Aug

at

Yeah

ok

maybe

immutability

is

not

the

real

problem

here

Though

I

can

imagine

that

registersized

integers

suffer

less

from

immutability

on

the

Pythonside

than

strings

do

But

then

I

think

that

preallocation

actually

is

the

whole

story

Debilski

Aug

at

Debiliski

The

preallocation

was

probably

the

missing

link

for

me

it

tells

why

join

behaves

so

much

better

than

a

generic

sum

I

had

to

look

at

the

code

to

understand

I

think

the

immutability

is

a

red

herring

Muhammad

Alkarouri

Aug

at

Debiliski

Unfortunately

I

have

accepted

the

other

answer

else

I

would

have

suggested

that

you

edit

yours

to

make

the

preallocation

and

explanation

more

prominent

Muhammad

Alkarouri

Aug

at

Does

it

actually

preallocate

Joins

argument

can

be

an

arbitrary

iterator

which

you

might

only

be

able

to

traverse

once

Without

being

familiar

with

the

code

Id

guess

the

key

point

is

at

the

C

level

youve

got

an

overallocated

array

which

you

can

mutate

in

place

since

it

hasnt

been

released

to

python

code

yet

this

doesnt

break

the

python

strings

are

immutable

law

The

repeated

allocationscopying

can

be

amortized

to

On

if

you

always

increase

the

size

by

at

least

double

when

you

need

more

rather

than

doing

it

for

every

string

Ben

Apr

at

add

a

comment

I

dont

know

why

but

this

works

import

operator

def

return

reduce

operator

add

answered

Oct

at

Dinesh

Panchananam

Because

reduce

is

not

sum

but

it

will

still

have

horrible

performance

for

large

lists

Ethan

Furman

Nov

at

add

a

comment

Not

the

answer

youre

looking

for

Browse

other

questions

tagged

python

sum

builtin

or

ask

your

own

question

asked

years

months

ago

viewed

times

active

month

ago

People

Chatting

SO

Close

Vote

Reviewers

mins

ago

Praveen

Kumar

Purushothaman

PHP

mins

ago

Wes

Hot

Network

Questions

Is

Lex

Luthors

theft

canonical

Should

we

add

a

character

in

a

story

for

realistic

effect

How

do

you

make

an

Int

or

Wisbased

character

with

only

Con

viable

with

hp

at

level

Can

a

spell

scroll

be

upcast

using

a

higherlevel

spell

slot

How

can

I

get

random

numbers

on

AVR

A

critic

made

a

comment

that

my

female

character

sounds

like

she

was

written

by

a

man

How

deep

must

be

a

pit

on

the

moon

to

hold

atmosphere

at

on

the

bottom

Find

if

a

list

is

an

ABCtriple

Flattening

directory

hierarchy

preserving

directory

names

in

new

directory

name

I

used

olive

oil

to

finish

my

salad

serving

utensils

now

what

Jean

Bourgains

Relatively

Lesser

Known

Significant

Contributions

Easier

way

to

find

amount

of

solutions

between

a

line

and

quadratic

Can

someone

read

my

EMail

if

I

lose

access

to

my

domain

Can

you

plug

the

computer

directly

into

the

wall

to

access

the

internet

The

reasoning

behind

doing

series

expansions

and

approximating

functions

in

physics

Where

Do

I

Come

From

And

Where

Am

I

Going

Daily

standup

vs

Micromanagement

Is

it

impolite

to

reject

new

tasks

while

Im

leaving

the

company

How

do

I

find

out

that

the

following

two

matrices

are

similar

What

ship

is

this

and

which

military

campaign

How

does

Expedia

manage

to

sell

a

room

significantly

cheaper

than

the

hotel

itself

What

does

The

grass

is

greener

on

the

other

side

mean

Half

my

house

losing

power

AC

the

key

factor

How

to

remove

unnecessary

fills

in

the

eagle

polygon

B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


Parameter specified as nonnull is null in ArrayAdaper Ask Question 7 Ive extended ArrayAdapter for spinner class OrderAdapter context Context resource Int objects List Order ArrayAdapter Order context resource objects override fun getView position Int convertView parent ViewGroup val view super getView position convertView parent view let view find TextView android R id text1 text getItem position name return override fun getDropDownView position Int convertView parent ViewGroup val view super getDropDownView position convertView parent view let view find TextView android R id text1 text getItem position name return Im getting exception java lang IllegalArgumentException Parameter specified as non null is null method kotlin jvm internal Intrinsics checkParameterIsNotNull parameter convertView at com github blabla endlesss ui adapter OrderAdapter getView OrderAdapter kt 0 Any ideas how to fix it android kotlin improve this question asked Feb 25 16 at 809 Dawid Hyży 1289 3 16 32 1 The exception is pretty clear You are passing null for the convertView parameter Arno Feb 25 16 at 818 1 This method is invoked by the Android framework and the parameter is actually defined as nullable so nulls should be allowed Kirill Rakhman Feb 25 16 at 1101 Dawid are you sure your code is the same you posted here Specifically is the convertView parameter really declared as nullable Kirill Rakhman Feb 25 16 at 1102 Is super getView with the same signature Jayson Minard Feb 25 16 at 1128 2 This was issue with incremental compile Ive rebuild project and it works Dawid Hyży Feb 25 16 at 1306 3 Answers oldest 5 This issue was caused by incremental compile Just needed to rebuild project after converting from Java answered Jun 29 17 at 1654 Dawid Hyży 1289 3 16 32 2 Please check the sample below override fun getDropDownView position Int convertView parent ViewGroup return getCustomView position convertView parent override fun getView position Int convertView parent ViewGroup return getCustomView position convertView parent fun getCustomView position Int convertView parent ViewGroup val rootView LayoutInflater from context inflate R layout my_custom_view parent false return rootView The returned value should be nullable answered Jun 22 16 at 1327 Denys Makhov 31 3 1 It was caused by incremental compile Dawid Hyży Jun 23 16 at 529 DawidHyży Pls post this your comment as answer and accept it Here happened the same stackoverflowcomquestions44800225 Willi Mentzel Jun 29 17 at 1008 2 override fun getView i Int view parent ViewGroup var itemView LayoutInflater from context inflate R layout item_dashbord_data parent false return itemView change name of View object itemView differ from getView second object view so change view to itemView or other name Ichigo Kurosaki 2610 7 27 43 answered Feb 21 18 at 813 Anand Raj Mehta 211 2 4 This is already resolved Dawid Hyży Feb 22 18 at 814 Not the answer youre looking for Browse other questions tagged android kotlin or ask your own question asked 2 years 10 months ago viewed 9065 times active 10 months ago Blog Adios to Winter Bash 2018 21 People Chatting SO Close Vote Reviewers 52 mins ago Radiodef SOBotics 53 mins ago Natty 0 Variable is declared as var but compiler is saying it val question feed

O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


python Matplotlib 3D input formatting Z dependent values 3 favorite I have a question quite similar to this one Matplotlib 3D plot 2D format for input data but it only partly solved my problem which was originally that plot_surface didnt recognize any arguments What I am trying to do is plot a function of 2 parameters and this is what I have fig plt figure ax fig add_subplot 111 projection 3d X Y np meshgrid positions 0 np transpose positions 1 Z data 0 Axes3D plot_surface X Y Z which throws this error TypeError plot_surface missing 1 required positional argument Z data0 is a 2D array data is a list of 2D arrays positions is a tuple of a row vector and a column vector so line 3 is essentially reconstructing positions as two row vectors and then making two matrices one for X and one for Y values as I believe Axes3Dplot_surface requires My problem is that even though Z is 2D array plot_surface refuses to recognize it as such Edit I just confirmed that Z is indeed the correct shape using this if Z shape X shape print Same shape Axes3D plot_surface X Y Z So I have no idea why it will not work Even when I use X for Z like this since it didnt complain about X missing or being the wrong shape it still gets mad Axes3D plot_surface X Y X Edit In looking around examples and trying things out I finally got it to work I hardly consider this solved though because I still have no idea why one way works and another doesnt and I dont want to leave this unsolved for posterity I got it to work by doing this surf ax plot_surface X Y Z instead of Axes3Dplotsurface I have no idea why this change got it to work so please still help me figure this out arrays python3x multidimensionalarray matplotlib 3d improve this question edited May 23 17 at 1225 Community 1 1 asked Mar 27 16 at 436 Mr HelloBye 23 1 8 This is kinda along the right lines stackoverflowcomquestions9170838 Mr HelloBye Mar 27 16 at 444 But the problem is that I already have the array previously generated I may rewrite it like this later but thats not what I need now I cant see why it wont plot as is Mr HelloBye Mar 27 16 at 445 1 I ran into this as well the documentation seems to suggest you can use it like this but you cant at all If you do Axes3Dplot_surfaceXXYYZZ youll see that its missing self This seems to be a version 100 change thats left the docs a bit confused OJFord Mar 19 17 at 1211 1 Answer active oldest 3 As you said it seems that the right way to do this is via axis object ax in the code below fig plt figure ax fig gca projection 3d ax plot_surface X Y Z Alternatively you can still call it using Axes3D but then you need to pass it the axis object ax like so Axes3D plot_surface ax X Y Z answered Jan 1 at 1837 S Sergei 31 2 Your Answer By clicking Post Your Answer you acknowledge that you have read our updated terms of service and cookie policy and that your continued use of the website is subject to these policies Not the answer youre looking for Browse other questions tagged arrays python3x multidimensionalarray matplotlib 3d or ask your own question

O O O O O O O O O O O O O O O O O O O B I I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


Docker

build

command

with

tag

unable

to

tag

images

Ask

Question

I

have

trying

to

build

a

Docker

image

using

a

Dockerfile

available

locally

docker

build

t

newimage

I

have

used

this

command

multiple

times

earlier

too

but

somehow

its

not

working

currently

and

i

am

stuck

finding

the

reason

for

it

I

will

be

really

helpful

if

someone

can

help

me

with

a

possible

solution

or

a

possible

area

to

look

for

an

issue

i

already

had

a

look

over

other

posts

that

could

have

been

related

for

example

Docker

build

tag

repository

name

docker

dockerfile

dockerbuild

improve

this

question

edited

May

at

Community

asked

Aug

at

Harshil

Whats

the

error

message

you

get

Martin

Aug

at

No

error

message

When

i

run

docker

images

there

is

not

image

with

such

tag

instead

i

have

an

image

tagged

Harshil

Aug

at

Then

it

seems

you

already

built

an

image

with

the

same

tag

but

a

different

Dockerfile

or

made

changes

to

it

Docker

does

not

delete

the

container

it

only

removes

the

tag

resulting

in

Martin

Aug

at

I

just

checked

it

I

tried

clearingremoving

all

the

images

Then

i

ran

docker

build

t

newimage

command

again

now

only

one

image

is

available

on

running

docker

images

and

that

is

tagged

with

Harshil

Aug

at

Thanks

for

looking

into

it

mgansler

Harshil

Aug

at

Answers

active

oldest

votes

Okay

I

found

out

the

reason

for

issue

DOCKER

BUILD

PROCESS

When

we

build

a

docker

image

while

creating

a

image

several

other

intermediate

images

are

generated

in

the

process

We

never

see

them

in

docker

images

because

with

the

generation

of

next

intermediate

image

the

earlier

image

is

removedAnd

in

the

end

we

have

only

one

which

is

the

final

image

The

tag

we

provide

using

t

or

tag

is

for

the

final

build

and

obviously

no

intermediate

container

is

tagged

with

the

same

ISSUE

EXPLANATION

When

we

try

to

build

a

docker

image

with

Dockerfile

sometimes

the

process

is

not

successfully

completed

with

a

similar

message

like

Successfully

built

image

with

IMAGEID

So

it

is

so

obvious

that

the

build

which

has

failed

will

not

be

listed

in

docker

images

Now

the

image

with

tag

is

some

other

image

intermediate

This

creates

a

confusion

that

the

image

exists

but

without

a

tag

but

the

image

is

actually

not

what

the

final

build

should

be

hence

not

tagged

edited

Nov

at

answered

Aug

at

Harshil

There

should

have

been

an

error

message

saying

that

the

build

was

not

successful

Did

you

not

see

that

Henry

Aug

at

No

there

was

no

such

message

I

made

a

typo

in

a

command

so

there

was

a

log

saying

the

command

returned

nonzero

output

binsh

s

not

found

The

command

binsh

c

s

returned

a

nonzero

code

something

like

this

but

there

was

no

error

message

from

Docker

daemon

Harshil

Aug

at

Theres

nothing

wrong

with

Docker

An

image

can

have

multiple

tags

alpine

weeks

ago

MB

alpine

latest

weeks

ago

MB

In

this

example

the

image

with

id

is

tagged

alpinelatest

and

If

you

were

to

execute

docker

build

t

alpine

the

latest

tag

would

be

removed

from

the

image

and

assigned

to

the

newly

built

image

which

has

a

different

id

If

you

remove

the

last

tag

from

an

image

the

image

isnt

deleted

automatically

It

shows

up

as

Docker

also

uses

a

cache

So

if

you

build

an

image

with

a

Dockerfile

change

that

file

built

the

image

again

and

than

undo

the

change

and

build

again

you

will

have

two

images

the

image

you

built

in

the

first

and

last

step

are

the

same

The

second

image

will

be

tagged

If

you

want

to

keep

multiple

version

of

an

image

use

docker

build

t

tagversion

imagetag

where

version

tag

is

changed

every

time

you

make

some

changes

What

I

called

tag

is

actually

the

image

name

and

what

I

called

version

is

called

the

tag

httpsdocsdockercomenginereferencecommandlinetag

edited

Aug

at

answered

Aug

at

Martin

the

information

you

shared

is

perfectly

fine

but

this

is

not

what

i

am

looking

for

It

is

not

a

problem

related

to

muliple

images

with

same

name

thanks

anyways

Harshil

Aug

at

Than

please

share

the

output

of

the

docker

build

command

as

well

as

docker

images

Martin

Aug

at

thanks

for

the

effort

i

found

out

the

issue

and

have

posted

the

answer

Harshil

Aug

at

One

of

walkaround

solution

for

this

case

run

below

command

immediately

after

build

command

if

all

other

images

has

already

been

taged

docker

tag

docker

image

ls

grep

awk

print

newimagelatest

As

I

know

this

will

work

only

in

linux

os

answered

Nov

at

Not

the

answer

youre

looking

for

Browse

other

questions

tagged

docker

dockerfile

dockerbuild

or

ask

your

own

question

asked

years

months

ago

viewed

times

active

month

ago

question

feed

B I I O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


How to only keep the time portion of a datetime value SQL Server Ask Question 1 Heres what I use to get a HHMM string CONVERTvarchar5 time_requested 108 Im thinking there may be a more elegant way even maybe more efficient see similar question on How to remove the time portion of a datetime value SQL Server Requirements the final result has to be easily convertible to a string in order to be able to concatenate some field time_created with a field such as date_created the following 2 cases must be covered HHMM and HHMMSS sqlserver datetime dateconversion improve this question edited May 23 17 at 1233 Community 1 1 asked Oct 22 16 at 1516 user3341592 216 5 20 CONVERTvarchar5 time_requested 108 I think as per your 1st requirement this is most elegant solution Which version of SQL SERVER you are using Pரதீப் Oct 22 16 at 1520 Using 2008 and 2012 user3341592 Oct 22 16 at 1605 2 Answers active oldest 2 Declare D datetime GetDate Data Type could be Time as well Select CONVERTvarchar8 D 108 for HHMMSS 112726 Select CONVERTvarchar5 D 108 for HHMM 1127 Failed to mention if 2012 you could also use Format Declare D DateTime 20161022 133025 Select FormatDHHmm 1330 Select FormatDhhmmss tt 013025 PM answered Oct 22 16 at 1528 John Cappelletti 444k 6 24 44 Didnt know about Format Thx user3341592 Oct 26 16 at 757 user3341592 It is a great function with many options HOWEVER the performance on large datasets is horrible John Cappelletti Oct 26 16 at 1344 2 This is too long for a comment Your method is fine It produces a string representation of the value You can also convert to a time data type select casttime_requested as time But note that this is formatless it contains hours minutes seconds and fractions of seconds So your method is probably the better approach answered Oct 22 16 at 1518 Gordon Linoff 760k 35 293 399 Thanks for the addedvalue comment user3341592 Oct 26 16 at 758 Your Answer By clicking Post Your Answer you acknowledge that you have read our updated terms of service and cookie policy and that your continued use of the website is subject to these policies Not the answer youre looking for Browse other questions tagged sqlserver datetime dateconversion or ask your own question asked 2 years 2 months ago viewed 1706 times active 2 years 2 months ago Blog Adios to Winter Bash 2018 question feed

O O O O O O O O O O O B I O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


Recursively delete 0KB files using windows cmd Ask Question 20 8 I have some process which creates some files of 0KB size in a directory and its subdirectories How can I delete the files from the file system using the windows command prompt Any single command or a script that will do the task will work I can only run simple cmd commands and scripts working on a remote machine with restricted access windows file cmd improve this question edited Nov 14 10 at 1013 asked Nov 14 10 at 942 crodjer 8231 7 28 49 1 What version of Windows Many versions of Windows include VBScript which might be more flexible suite101comcontent Klinky Nov 14 10 at 1021 I would use for to scan dir tree for files findstr to search files for pattern and delete those nonmatching Constantin Nov 14 10 at 1032 4 Answers active oldest votes 44 Iterate recursively over the files for r F in Find out zerolength files if zF0 Delete them del F Putting it all together for r F in do if zF0 del F If you need this in a batch file then you need to double the for r F in do if zF0 del F Note I was assuming that you meant files of exactly 0 Bytes in length If with 0 KB you mean anything less than 1000 bytes then above if needs to read if zF LSS 1000 or whatever your threshold is answered Nov 14 10 at 1054 Joey 262k 62 559 597 hmm that worked thanks crodjer Nov 14 10 at 1120 1 For completeness Same command turning off output stackoverflowcoma180149122436175 Antonio Aug 2 13 at 1156 Use if if you want to suppress the printing of the if statement for every file Kip Mar 20 17 at 1501 I think the issue with this solution is that it also deletes also very small files not empty ones Foad Sep 9 18 at 2337 1 Foad the if zF0 will only catch empty files Joey Sep 10 18 at 711 show 2 more comments 2 echo off setLocal EnableDelayedExpansion for f tokens delims a in dirsbad do if Za equ 0 del a Found at seems to work with one caveat It wont delete files with names containing spaces There may be a workaround but Im afraid batch is not my forte answered Nov 14 10 at 1105 Northover 938 8 14 Well since you explicitly declare space to be a token delimiter for will tokenize I guess del a b should work But the actual answer is that you should never use for f over the output of dir unless it is absolutely necessary eg for sorting for itself can iterate over files and is much more robust that way For example your solution wont handle file names that contain Unicode characters with no representation in the OEM codepage if Raster fonts are selected for the console window Those interactions are subtle but I tend to stay far away from for f for filenames Joey Nov 14 10 at 1448 0 This works fine when a typo is corrected The problem was a missing tilde eg del a needs to be del a This will indeed delete files with spaces in the name because it encloses the token in double quotes an alternate method would be to use short name as shown in the second example sa echo off setLocal EnableDelayedExpansion for f tokens delims a in dirsbad do if Za equ 0 del a echo off setLocal EnableDelayedExpansion for f tokens delims a in dirsbad do if Za equ 0 del sa answered May 24 13 at 1327 Thom Spengler 1 1 You can try findexe from the UnxUtils find type f empty delete answered Nov 14 10 at 955 Benoit 588k 15 164 212 Its not useful for this purposeworking a remote machineso no direct access Can only run cmd commands and scripts crodjer Nov 14 10 at 1006 I think this answer shouldnt have been downvoted It still provides a useful bit of information probably for slightly different contexts Upvoted to neutralize it crodjer May 24 13 at 1720 Not the answer youre looking for Browse other questions tagged windows file cmd or ask your own question asked 8 years 1 month ago viewed 29233 times active 1 year 7 months ago Blog Adios to Winter Bash 2018 question feed

O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


Reading XML Data using SqlDataReader Ask Question 0 I have a stored procedure that creates an XML document I then have the following code using SqlConnection con new SqlConnection _connectionString con Open using SqlCommand cmd new SqlCommand GetModuleInstallerManifestXML con cmd CommandType System Data CommandType StoredProcedure cmd Parameters Add new SqlParameter mod_name SqlDbType VarChar 250 Value this ModuleName using SqlDataReader reader cmd ExecuteReader CommandBehavior CloseConnection while reader manifestXmlList Add reader I need to add the xml data row by row into a list and then write that list to a file how do I do this c sqlserver xml adonet improve this question edited Mar 22 17 at 1510 marc_s 571k 128 1103 1251 asked Mar 22 17 at 1500 Mattaceten 49 1 10 1 Answer oldest 1 check this answer httpsstackoverflowcoma54242505358389 string xmlString string Empty using XmlReader reader cmd ExecuteXmlReader XDocument xml XDocument Load reader x Save filePath Community 1 1 answered Mar 22 17 at 1526 daniell89 1017 8 20 Thanks for the help Mattaceten Mar 23 17 at 1556 Your Answer By clicking Post Your Answer you acknowledge that you have read our updated terms of service and cookie policy and that your continued use of the website is subject to these policies Not the answer youre looking for Browse other questions tagged c sqlserver xml adonet or ask your own question asked 1 year 9 months ago viewed 1852 times active 1 year 9 months ago Linked 17 How do I return XML from a Stored Procedure Hot Network Questions How can I provide an electrical outlet at my portable kitchen island Where did the concept of Passive Insight come from Synonym for a helpseeker Is it impolite to reject new tasks while Im leaving the company Length of the Longest Descent A critic made a comment that my female character sounds like she was written by a man Understanding the purported ambiguity in Every boy didnt run Postdoc with a professor retiring soon Half my house losing power AC the key factor Unbookable column of seats on 777200 What is a good way to handle lengthy monologueslectures in a novel Was there any figure considered to be evil in iron age Celtic paganism religion of England A Very Illegal Position I keep rolling terribly How can I keep playing this character and have fun despite my luck A Romantechnology clock run by slaves how to make it accurate Why are probes that tend to explore outer system always launched to go outwards instead of straight upwards or downwards Reviewer signs name on review Should the editor censor Short story read 25 year ago where changing persons name prevents a war Can someone read my EMail if I lose access to my domain New manager is a jerk is it OK to quit without providing a reason Why is angle of incidence equal to angle of reflection Could we use a narrow paradigm say laser to get info faster from New Horizons With a moon base Find if a list is an ABCtriple DiffieHellman Group 25 and 26 question feed

O O O O B O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


BeautifulSoup and lxmlhtml what to prefer duplicate 26 9 This question already has an answer here Parsing HTML in python lxml or BeautifulSoup Which of these is better for what kinds of purposes 7 answers I am working on a project that will involve parsing HTML After searching around I found two probable options BeautifulSoup and lxmlhtml Is there any reason to prefer one over the other I have used lxml for XML some time back and I feel I will be more comfortable with it however BeautifulSoup seems to be much common I know I should use the one that works for me but I was looking for personal experiences with both python beautifulsoup lxml improve this question asked Feb 11 11 at 849 user225312 625k 56 144 171 marked as duplicate by kay tiago oefe zero323 halfer Dec 4 13 at 2040 This question has been asked before and already has an answer If those answers do not fully address your question please ask a new question 4 Answers oldest 15 In summary lxml is positioned as a lightningfast productionquality html and xml parser that by the way also includes a soupparser module to fall back on BeautifulSoups functionality BeautifulSoup is a oneperson project designed to save you time to quickly extract data out of poorlyformed html or xml lxml documentation says that both parsers have advantages and disadvantages For this reason lxml provides a soupparser so you can switch back and forth Quoting BeautifulSoup uses a different parsing approach It is not a real HTML parser but uses regular expressions to dive through tag soup It is therefore more forgiving in some cases and less good in others It is not uncommon that lxmllibxml2 parses and fixes broken HTML better but BeautifulSoup has superiour support for encoding detection It very much depends on the input which parser works better In the end they are saying The downside of using this parser is that it is much slower than the HTML parser of lxml So if performance matters you might want to consider using soupparser only as a fallback for certain cases If I understand them correctly it means that the soup parser is more robust it can deal with a soup of malformed tags by using regular expressions whereas lxml is more straightforward and just parses things and builds a tree as you would expect I assume it also applies to BeautifulSoup itself not just to the soupparser for lxml They also show how to benefit from BeautifulSoup s encoding detection while still parsing quickly with lxml from BeautifulSoup import UnicodeDammit def decode_html html_string converted UnicodeDammit html_string isHTML True if not converted unicode raise UnicodeDecodeError Failed to detect encoding tried s join converted triedEncodings print convertedoriginalEncoding return converted unicode root lxml html fromstring decode_html tag_soup Same source httplxmldeelementsouphtml In words of BeautifulSoup s creator Thats it Have fun I wrote Beautiful Soup to save everybody time Once you get used to it you should be able to wrangle data out of poorlydesigned websites in just a few minutes Send me email if you have any comments run into problems or want me to know about your project that uses Beautiful Soup Leonard Quoted from the Beautiful Soup documentation I hope this is now clear The soup is a brilliant oneperson project designed to save you time to extract data out of poorlydesigned websites The goal is to save you time right now to get the job done not necessarily to save you time in the long term and definitely not to optimize the performance of your software Also from the lxml website lxml has been downloaded from the Python Package Index more than two million times and is also available directly in many package distributions eg for Linux or MacOSX And from Why lxml The C libraries libxml2 and libxslt have huge benefits Standardscompliant Fullfeatured fast fast FAST lxml is a new Python binding for libxml2 and libxslt answered Oct 23 13 at 1748 osa 4371 30 42 2 Use both lxml for DOM manipulation BeautifulSoup for parsing httplxmldeelementsouphtml twasbrillig 6152 4 24 52 answered Feb 11 11 at 1034 ymv 1637 9 19 1 What do you mean by parsing I ask this because IMHO parsing is just the same as performing operations on the DOM nn0p Aug 24 15 at 505 0 lxmls great But parsing your input as html is useful only if the dom structure actually helps you find what youre looking for Can you use ordinary string functions or regexes For a lot of html parsing tasks treating your input as a string rather than an html document is counterintuitively way easier answered Feb 11 11 at 1130 dfichter 968 7 9 3 easier perhaps but not robust by any means Its extremely easy for a formatting change in the input HTML line wrapping whitespace element encoding etc to break a manuallydeveloped parser If you want to build something to parse input you dont control or which otherwise might change in the future using a real HTML parser is the Right Thing Charles Duffy Aug 30 11 at 1722 6 dfichter Youve done it again You spake the unspeakable youve uttered the unholy incantation by crossing html and regexes in the same breath Youve surely wandered into the mouth of madness as so many poor souls before you aculich Jan 16 12 at 2126 Not the answer youre looking for Browse other questions tagged python beautifulsoup lxml or ask your own question

B O O O O O O O O O O O O O O O O O O O B O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O B B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O B O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O B O O O O O O O B O O O O O O O B O O O O O O O O B O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O B O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B B O O O O O


Calling

one

Activity

from

another

in

Android

Ask

Question

How

can

I

call

another

activity

from

one

the

current

activity

And

for

the

same

I

want

to

call

an

activity

which

contains

a

dialog

message

box

from

my

current

activity

android

improve

this

question

Tony

asked

Mar

at

OnkarDhane

This

is

an

exact

duplicate

of

your

previous

question

RivieraKid

Mar

at

Answers

oldest

First

question

Use

the

Intent

to

call

another

Activity

In

the

Manifest

you

should

add

activity

androidname

ListViewImage

activity

activity

androidname

comcompanylistviewListViewImage

activity

And

in

your

current

activity

btListe

ImageButton

findViewById

R

id

btListe

setOnClickListener

new

OnClickListener

public

void

onClick

v

intent

new

Intent

main

this

ListViewImage

class

startActivity

intent

finish

Second

question

sendButton

setOnClickListener

new

OnClickListener

public

void

onClick

v

String

valueString

editValue

getText

toString

long

value

if

valueString

value

Long

parseLong

valueString

else

value

Bundle

sendBundle

new

Bundle

sendBundle

putLong

value

value

Intent

i

new

Intent

this

class

i

putExtras

sendBundle

startActivity

i

finish

and

in

Bundle

receiveBundle

this

getIntent

getExtras

final

long

receiveValue

receiveBundle

getLong

value

receiveValueEdit

setText

String

valueOf

receiveValue

callReceiverButton

setOnClickListener

new

OnClickListener

public

void

onClick

v

Intent

i

new

Intent

this

Receiver

class

i

putExtra

new

value

receiveValue

Potheek

answered

Mar

at

LTEHUB

heyhithanks

but

please

tell

me

if

i

want

to

show

popup

message

after

completing

my

progress

bar

which

is

on

activity

now

i

have

created

progress

bar

its

runs

but

remaining

part

i

want

to

show

popmessage

as

progress

bar

reached

the

max

limitplease

help

me

out

OnkarDhane

Mar

at

LTEHUB

Mar

at

check

the

following

code

to

call

one

activity

from

another

Intent

intent

new

Intent

CurrentActivity

this

OtherActivity

class

CurrentActivity

this

startActivity

intent

answered

Mar

at

Tushar

Vengurlekar

I

used

following

code

on

my

sample

application

to

start

new

activity

Button

Button

findViewById

R

id

TEST

setOnClickListener

new

OnClickListener

public

void

onClick

Intent

myIntent

new

Intent

getContext

MyActivity

class

startActivityForResult

myIntent

answered

Jan

at

Kunal

As

we

dont

know

what

are

the

names

of

your

activities

classes

lets

call

your

current

activity

and

the

one

you

wish

to

open

these

are

the

names

of

the

classes

First

you

need

to

define

an

intent

that

will

be

sent

to

Intent

new

Intent

this

class

Then

you

can

simply

launch

the

activity

by

running

answered

Mar

at

Erez

A

Korn

thnksnow

in

activity

i

have

progress

bar

as

progress

bar

completesreached

max

value

then

i

want

to

show

popmessage

in

another

activityplease

tell

me

how

to

achieve

popmessage

thing

in

activitymy

activity

is

overtell

me

please

OnkarDhane

Mar

at

What

you

need

is

to

push

some

data

with

the

intent

that

will

let

know

that

it

need

to

show

something

Then

handle

the

incoming

data

in

Activity

Erez

A

Korn

Mar

at

Use

context

instead

of

Class

name

Nirav

Joshi

Oct

at

Add

the

following

to

your

on

button

click

event

Intent

intent

new

Intent

First

this

Second

class

startActivity

intent

finish

answered

Aug

at

Nirav

Joshi

This

task

can

be

accomplished

using

one

of

the

androids

main

building

block

named

as

Intents

and

One

of

the

methods

public

void

startActivity

Intent

intent

which

belongs

to

your

Activity

class

An

intent

is

an

abstract

description

of

an

operation

to

be

performed

It

can

be

used

with

startActivity

to

launch

an

Activity

broadcastIntent

to

send

it

to

any

interested

BroadcastReceiver

components

and

startServiceIntent

or

bindServiceIntent

ServiceConnection

int

to

communicate

with

a

background

Service

An

Intent

provides

a

facility

for

performing

late

runtime

binding

between

the

code

in

different

applications

Its

most

significant

use

is

in

the

launching

of

activities

where

it

can

be

thought

of

as

the

glue

between

activities

It

is

basically

a

passive

data

structure

holding

an

abstract

description

of

an

action

to

be

performed

Refer

the

official

docs

httpdeveloperandroidcomreferenceandroidcontentIntenthtml

public

void

startActivity

Intent

intent

Used

to

launch

a

new

activity

So

suppose

you

have

two

Activity

class

and

on

a

button

clicks

OnClickListener

you

wanna

move

from

one

Activity

to

another

then

PresentActivity

This

is

your

current

activity

from

which

you

want

to

go

the

second

activity

NextActivity

This

is

your

next

Activity

on

which

you

want

to

move

It

may

contain

anything

like

you

are

saying

dialog

box

So

the

Intent

would

be

like

this

Intent

PresentActivity

this

NextActivity

class

Finally

this

will

be

the

complete

code

public

class

PresentActivity

extends

Activity

protected

void

onCreate

Bundle

icicle

super

onCreate

icicle

setContentView

R

layout

final

Button

button

Button

findViewById

R

id

button

setOnClickListener

new

OnClickListener

public

void

onClick

v

Perform

action

on

click

Intent

activityChangeIntent

new

Intent

PresentActivity

this

NextActivity

class

currentContextstartActivityactivityChangeIntent

PresentActivity

this

startActivity

activityChangeIntent

This

exmple

is

related

to

button

click

you

can

use

the

code

anywhere

which

is

written

inside

button

clicks

OnClickListener

at

any

place

where

you

want

to

switch

between

your

activities

answered

May

at

Nikhil

Agrawal

Put

this

inside

the

onCreate

method

of

Button

btnEins

Button

findViewById

R

id

btnEins

setOnClickListener

new

OnClickListener

Override

public

void

onClick

v

Intent

intencion

new

Intent

v

getContext

class

startActivity

intencion

answered

Jul

at

Weapon

X

I

have

implemented

this

way

and

it

worksIt

is

much

easier

than

all

that

is

reported

We

have

two

activities

one

is

the

main

and

another

is

the

secondary

In

secondary

activity

which

is

where

we

want

to

end

the

main

activity

define

the

following

variable

public

static

Activity

ACTIVIDAD

And

then

the

following

method

public

static

void

enlaceActividadPrincipal

Activity

actividad

tuActividad

ACTIVIDAD

actividad

Then

in

your

main

activity

from

the

onCreate

method

you

make

the

call

actividadSecundaria

enlaceActividadPrincipal

this

Now

youre

in

control

Now

from

your

secondary

activity

you

can

complete

the

main

activity

Finish

calling

the

function

like

this

ACTIVIDAD

finish

Christopher

Bottoms

answered

Jun

at

PacoTron

The

following

code

demonstrates

how

you

can

start

another

activity

via

an

intent

Start

the

activity

with

an

intent

connected

to

the

specified

class

Intent

i

new

Intent

this

ActivityTwo

class

startActivity

i

Activities

which

are

started

by

other

Android

activities

are

called

subactivities

This

wording

makes

it

easier

to

describe

which

activity

is

meant

edited

Jan

at

Michael

Fourre

answered

Jan

at

you

should

read

how

post

code

in

meta

Divyesh

Kanzariya

Jan

at

check

the

following

code

to

open

new

activit

Intent

f

new

IntentMainActivitythis

SecondActivityclass

startActivityf

edited

Nov

at

answered

Nov

at

Farooq

Ajaz

Very

easiest

way

to

call

one

activity

to

another

is

startActivity

new

Intent

getApplicationContext

YourActivity

class

answered

Aug

at

Galvanize

Monad

This

is

the

same

solution

of

the

other

answers

also

suggest

I

would

suggest

to

delete

this

answer

since

it

doesnt

add

anything

new

with

regards

to

older

answers

Robin

Topper

Aug

at

a

silly

different

between

solution

so

something

optimized

than

solution

Topper

Galvanize

Monad

Aug

at

Not

the

answer

youre

looking

for

Browse

other

questions

tagged

android

or

ask

your

own

question

asked

years

months

ago

viewed

times

active

months

ago

Blog

Adios

to

Winter

Bash

question

feed

B I I O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O


Stopping

the

debugger

when

a

floating

point

number

is

produced

Ask

Question

I

have

an

C

program

Somewhere

in

the

program

hard

to

reproduce

but

reproduceable

a

caclculation

results

in

a

float

beeing

set

to

a

Since

a

floating

point

operation

involving

a

results

in

a

this

spreads

fast

Is

there

any

way

I

can

setup

the

compiler

gcc

or

the

debuger

gdb

to

stop

when

a

floating

point

operation

results

in

a

That

would

be

extremely

useful

Thanks

Nathan

PS

It

might

matter

I

am

working

under

ubuntu

linux

c

gdb

floatingpoint

improve

this

question

edited

Nov

at

asked

Mar

at

Nathan

You

could

enable

floating

point

exceptions

just

in

debug

mode

Check

your

CRT

not

sure

how

it

is

done

in

yours

Hans

Passant

Mar

at

See

also

Riot

Oct

at

Answer

active

votes

You

could

enable

floating

point

exceptions

see

glibc

Control

Functions

then

youll

get

a

SIGFPE

when

your

value

is

produced

answered

Mar

at

Erik

Does

the

trick

thank

you

Nathan

Mar

at

The

answer

is

correct

However

on

OSX

you

need

something

like

this

wwwpersonalumicheduwilliamsarchivecomputation

because

feenableexcept

is

not

available

fferri

Dec

at

Your

Answer

By

clicking

Post

Your

Answer

you

acknowledge

that

you

have

read

our

updated

terms

of

service

and

cookie

policy

and

that

your

continued

use

of

the

website

is

subject

to

these

policies

Not

the

answer

youre

looking

for

Browse

other

questions

tagged

c

gdb

floatingpoint

or

ask

your

own

question

asked

years

months

ago

viewed

times

active

years

months

ago

Blog

Adios

to

Winter

Bash

question

feed

O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


R

Cut

by

Defined

Interval

Ask

Question

Is

there

some

way

in

R

to

cut

by

a

defined

interval

without

any

breaks

For

example

if

I

want

the

values

in

the

exact

interval

by

default

cut

breaks

this

interval

into

smaller

intervals

r

improve

this

question

joran

asked

Apr

at

Barata

Answers

oldest

To

cut

unto

predefined

intervals

you

can

specify

a

vector

of

breaks

using

the

breaks

parameter

Define

some

data

x

replaceTRUE

x

Now

cut

x

at

and

cutx

includelowestTRUE

answered

Apr

at

Andrie

I

have

a

question

related

to

cut

gives

me

Levels

How

can

we

extract

the

cutoff

points

The

cut

off

points

here

are

darkage

Aug

at

darkage

Thats

a

different

question

not

really

related

to

the

original

But

your

question

is

answered

in

the

very

last

example

of

cut

If

that

doesnt

help

please

ask

a

new

question

Andrie

Aug

at

Something

like

this

Breaks

for

each

from

to

a

cuta

seqfrom

to

by

answered

Apr

at

Roman

Luštrik

Your

Answer

By

clicking

Post

Your

Answer

you

acknowledge

that

you

have

read

our

updated

terms

of

service

and

cookie

policy

and

that

your

continued

use

of

the

website

is

subject

to

these

policies

Not

the

answer

youre

looking

for

Browse

other

questions

tagged

r

or

ask

your

own

question

asked

years

months

ago

viewed

times

active

years

months

ago

Blog

Adios

to

Winter

Bash

question

feed

O O O B I O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


Using the keyword this in java duplicate Ask Question 19 11 This question already has an answer here When should I use this in a class 17 answers Im trying to get an understanding of what the the java keyword this actually does Ive been reading Suns documentation but Im still fuzzy on what this actually does java keyword improve this question edited Feb 23 09 at 1327 Matt Briggs 289k 10 80 122 asked Feb 23 09 at 1311 BloodParrot 153 1 2 7 marked as duplicate by Pshemo java Dec 30 15 at 310 This question has been asked before and already has an answer If those answers do not fully address your question please ask a new question 1 JasonC questions can only be duplicates of other questions in this site Luiggi Mendoza Aug 18 14 at 1555 12 Answers oldest 38 The this keyword is a reference to the current object class Foo private int bar public Foo int bar the this keyword allows you to specify that you mean this type and reference the members of this type in this instance it is allowing you to disambiguate between the private member bar and the parameter bar passed into the constructor this bar bar Another way to think about it is that the this keyword is like a personal pronoun that you use to reference yourself Other languages have different words for the same concept VB uses Me and the Python convention as Python does not use a keyword simply an implicit parameter to each method is to use self If you were to reference objects that are intrinsically yours you would say something like this My arm or my leg Think of this as just a way for a type to say my So a psuedocode representation would look like this class Foo private int bar public Foo int bar my bar bar edited Feb 23 09 at 1325 answered Feb 23 09 at 1314 Andrew Hare 275k 53 575 599 1 You could do the above without this for example using something like hungarian notation m_bar bar So does not really explain what this is really important for ng Feb 23 09 at 1318 1 could it be to avoid strange constructs like that Magnar Feb 23 09 at 1325 ng What do you do when you cant rename the field Like when its a protected field from a super class Aaron Digulla Feb 23 09 at 1337 ng You are correct that my usage of this could easily be replaced with careful naming conventions However I believe that I gave the most simple example as it seems clear that the OP is just learning OOP and may have trouble understanding a more advanced usage of this Andrew Hare Feb 23 09 at 1340 Andrew Very nice explanation EricSchaefer Feb 23 09 at 1410 show 2 more comments 21 The keyword this can mean different things in different contexts thats probably the source of your confusion It can be used as a object reference which refers to the instance the current method was called on return this It can be used as a object reference which refers to the instance the current constructor is creating eg to access hidden fields MyClass String name this name name It can be used to invoke a different constructor of a a class from within a constructor MyClass this default name It can be used to access enclosing instances from within a nested class public class MyClass String name public class MyClass String name public String getOuterName return MyClass this name answered Feb 23 09 at 1334 Michael Borgwardt 293k 63 426 662 1Does this only used and written through constructor 2 what about thissetTittleTittle of Fram as you can see it is used to invoke method but I didnt understand its semantic user2019510 Sep 5 13 at 2011 user2019510 no it is not only used in constructors my answer contains examples for different uses And yes its used to invoke methods in that case it means the current object and works just like using any other object reference to invoke a method Michael Borgwardt Sep 5 13 at 2016 thank you for kind help Michael user2019510 Sep 6 13 at 1818 add a comment 6 this is a reference to the current object See details answered Feb 23 09 at 1313 Otávio Décio 619k 13 140 209 add a comment 5 The keyword this is a reference to the current object Its best explained with the following piece of code public class MyClass public void testingThis You can access the stuff below by using this although this is not mandatory System out println this myInt System out println this myStringMethod Will print out 100 Hello World int myInt 100 string myStringMethod return Hello World Its not used a lot unless you have code standard at your place telling you to use the this keyword There is one common use for it and thats if you follow a code convention where you have parameter names that are the same as your class attributes public class ProperExample private int numberOfExamples public ProperExample int numberOfExamples this numberOfExamples numberOfExamples One proper use of the this keyword is to chain constructors making constructing object consistent throughout constructors public class Square public Square this 0 0 public Square int x_and_y this x_and_y x_and_y public Square int x int y finally do something with x and y This keyword works the same way in eg C edited Feb 23 09 at 1359 answered Feb 23 09 at 1316 Spoike 602k 37 125 151 This code is incorrect a static method has no reference to this ng Feb 23 09 at 1319 does it compile accessing object elements from a static method should not chburd Feb 23 09 at 1319 No it doesnt main is a static method and so there is no this pointer because there is no object that is invoking it This should not compile James Feb 23 09 at 1320 whoops I wrote an example in haste fixing it Spoike Feb 23 09 at 1321 This is another poor example of how this can be used and why its fundamentally important to the OO paradigm ng Feb 23 09 at 1325 show 5 more comments 4 An even better use of this public class Blah implements Foo public Foo getFoo return this It allows you to specifically this object in the current context Another example public class Blah public void process Foo foo foo setBar this How else could you do these operations answered Feb 23 09 at 1316 ng 5830 32 40 For your first example it really makes no sense to return this from an object The caller already has a reference to your object how else would he invoke the method eljenso Feb 23 09 at 1400 eljenso If return this is the only line in the method then yes its pretty useless But if its eg a setter it allows you to do method chaining myFoosetBarmyBarsetBazmyBazsetMyInt1 Michael Myers Feb 23 09 at 1506 Not only that but what if returning this is a characteristic of an interface implementation I challenge you to show me a single complex project that does not need to return this at some point Its a critical requirement of most OO systems ng Feb 23 09 at 1752 mmyers I agree I forgot about that one eljenso Feb 23 09 at 1802 ng Every caller invokes getFoo on a subtype of Foo Why the method if you can do Blah blah new Blah Foo foo blah Maybe we can turn things around and you can show me a project where this is returned as you described so I can better understand what youre trying to say eljenso Feb 23 09 at 1818 show 4 more comments 2 this keyword refers to current object due to which the method is under execution It is also used to avoid ambiguity between local variable passed as a argument in a method and instance variable whenever instance variable and local variable has a same name Example public class ThisDemo1 public static void main String args A a1 new A 4 5 class A int num1 int num2 A int num1 this num1 num1 here this refers to instance variable num1 this avoids ambigutiy between local variable num1 instance variable num1 System out println num1 this num1 A int num int num2 this num here this calls 1 argument constructor within the same class this num2 num2 System out println num2 this num2 Above line prints value of the instance variable num2 answered May 10 13 at 935 Priyanka Vishwakarma 309 2 6 add a comment 1 The keyword this refers to the current objects context In many cases as Andrew points out youll use an explicit this to make it clear that youre referring to the current object Also from this and super There are other uses for this Sometimes when you are writing an instance method you need to pass the object that contains the method to a subroutine as an actual parameter In that case you can use this as the actual parameter For example if you wanted to print out a string representation of the object you could say Systemoutprintlnthis Or you could assign the value of this to another variable in an assignment statement In fact you can do anything with this that you could do with any other variable except change its value That site also refers to the related concept of super which may prove to be helpful in understanding how these work with inheritance edited May 23 17 at 1225 Community 1 1 answered Feb 23 09 at 1327 Joe Liversedge 3526 22 19 add a comment 1 Its a reference of actual instance of a class inside a method of the same class coding public class A int attr 10 public int calc return this getA 10 get and set end class A In calc body the software runs a method inside the object allocated currently How its possible that the behaviour of the object can see itself With the this keyword exactly Really the this keyword not requires a obligatory use as super because the JVM knows where call a method in the memory area but in my opinion this make the code more readeable answered Feb 23 09 at 1337 alepuzio 1185 2 23 31 add a comment 1 It can be also a way to access information on the current context For example public class OuterClass public static void main String args OuterClass oc new OuterClass OuterClass InnerClass ic new InnerClass this class InnerClass InnerClass OuterClass oc System out println Enclosing class oc oc getClass System out println This class this this getClass System out println Parent of this class this getClass getEnclosingClass System out println Other way to parent OuterClass this answered Feb 23 09 at 1351 PhiLho 348k 3 78 121 add a comment 0 Think of it in terms of english this object is the object you currently have WindowMaker foo new WindowMaker this For example you are currently inside a class that extends from the JFrame and you want to pass a reference to the WindowMaker object for the JFrame so it can interact with the JFrame You can pass a reference to the JFrame by passing its reference to the object which is called this answered Feb 23 09 at 1330 Kyle G 392 3 13 add a comment 0 Every object can access a reference to itself with keyword this sometimes called the this reference First lets take a look on code public class Employee private int empId private String name public int getEmpId return this empId public String getName return this name public void setEmpId int empId this empId empId public void setName String name this name name In the above method getName return instance variable name Now lets take another look of similar code is public class Employee private int empId private String name public int getEmpId return this empId public String getName String name Yasir Shabbir return name public void setEmpId int empId this empId empId public void setName String name this name name public static void main String args Employee e new Employee e setName Programmer of UOS System out println e getName Output Yasir Shabbir this operator always work with instance variableBelong to Object not any class variableBelong to Class this always refer to class non static attribute not any other parameter or local variable this always use in non static method this operator can not work on static variableClass variable NOTEIts often a logic error when a method contains a parameter or local variable that has the same name as a field of the class In this case use reference this if you wish to access the field of the classotherwise the method parameter or local variable will be referenced answered Jul 26 14 at 1130 Yasir Shabbir Choudhary 2032 21 26 add a comment 0 What this does is very simply It holds the reference of current object This keyword holds the reference of instance of current class This keyword can not be used inside static function or static blocks This keyword can be used to access shadowed variable of instance This keyword can be used to pass current object as parameter in function calls This keyword can be used to create constructor chain Source httpjavaandmecomcorejavathisword answered Oct 10 15 at 1809 Ajay 1 add a comment Not the answer youre looking for Browse other questions tagged java keyword or ask your own question asked 9 years 10 months ago viewed 57386 times active 3 years 2 months ago Blog Adios to Winter Bash 2018 26 People Chatting SO Close Vote Reviewers 1 hour ago sideshowbarker SOBotics 1 hour ago Queen Hot Network Questions Does Linux kernel create a file for a internet domain socket I got accepted into a math PhD program but I dont feel adequate enough to attend I used olive oil to finish my salad serving utensils now what Is there any way to explore Gerudo Town without wearing the female disguise Interviewing spouse of a fired employee Why dont the Democrats make a deal to give Trump his border wall in exchange for campaign finance reform I keep rolling terribly How can I keep playing this character and have fun despite my luck Using macOS without an Apple ID Asking about eating policy during interview Is the Avian species from the game Starbound plausible How do I know where to put stitching vias Are these claims about Japans health system true Attempt on modified harmonic series What are the On and Off beats Parse all strings of specific length Why is angle of incidence equal to angle of reflection Can the Sleep spell take effect later once the Hit Point threshold is crossed Preparing in case ATMdebit cards might go missing during a long trip Why would a previously spacefaring civilization become unable to build another interstellar ship Air Serbia denied return flight after not taking outbound one Will my cancer ruin my chances of going to graduate school in math Why are these words suspicious How many stages are there in Samadhi Does Patanjali yoga sutras mention different stages of Samadhi Would Carbonbased Lifeforms Be Able to Eat Siliconbased Lifeforms

O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O B I O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I B I O O O O O O O O B I O O O O O O O O O O B I O O O O O O O O O B I O O O O O B I O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


Changing a SQL column title via query Ask Question 7 3 I have the following query SELECT product_description name product quantity product price product_option_value_description name product_option_value quantity FROM product INNER JOIN product_description ON product product_id product_description product_id INNER JOIN product_option_value_description ON product product_id product_option_value_description product_id INNER JOIN product_option_value ON product product_id product_option_value product_id ORDER BY product_description name How could I change the title for product_option_value_descriptionname as I would like to name this option mysql sql improve this question Neil Knight 358k 19 107 177 asked Jul 25 11 at 649 Jess McKenzie 4159 20 80 141 4 Answers oldest 20 Use an alias like so product_option_value_description name AS Option If you want to change the columns name not only for this query but in general use ALTER TABLE ALTER TABLE product_option_value_description CHANGE name newname DATATYPE answered Jul 25 11 at 650 Jacob 304k 3 60 76 1 If Im using AS is there an option to not return the original column name The default behavior appears to return both the original and the new column AndrewJM Oct 20 14 at 1524 7 Use as For example product_description name as ProdName product quantity product price product_option_value_description name as ProdDesc product_option_value quantity FROM product answered Jul 25 11 at 652 NightWolf 4266 7 51 104 6 Just write product_option_value_descriptionname AS Name to create the alias Name for this column answered Jul 25 11 at 650 JK 4713 1 20 23 1 You can use any alias name using the AS keyword eg select student_id AS id from student_info answered Jul 28 15 at 1847 Orson DCunha 21 11 Your Answer By clicking Post Your Answer you acknowledge that you have read our updated terms of service and cookie policy and that your continued use of the website is subject to these policies Not the answer youre looking for Browse other questions tagged mysql sql or ask your own question asked 7 years 5 months ago viewed active 3 years 5 months ago Blog Adios to Winter Bash 2018 question feed

O O B B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O


MySQL add days to a date Ask Question 34 6 I have a table in MySQL What would be the sql statement look like to add say 2 days to the current date value in the table UPDATE classes SET date date 1 where id 161 this adds one second to the value i dont want to update the time i want to add two days mysql sql improve this question edited Jan 3 16 at 814 Rish 736 4 17 asked Oct 19 11 at 1103 Beginner 101k 58 140 213 possible duplicate of Add 30 days for date in db JJJ Oct 19 11 at 1132 Possible duplicate of Add 30 days for date in db Andrew Dec 18 18 at 1734 asked 7 years 2 months ago viewed 80558 times active 14 days ago question feed

B O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O